Skip to content

Commit 6651bf2

Browse files
committed
Complete pong implementation :D
1 parent 4510c71 commit 6651bf2

13 files changed

+416
-31
lines changed
+85-23
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,69 @@
11
package uk.org.ulcompsoc.ld30;
22

3+
import uk.org.ulcompsoc.ld30.components.Ball;
4+
import uk.org.ulcompsoc.ld30.components.CheatingAI;
5+
import uk.org.ulcompsoc.ld30.components.MouseTracker;
36
import uk.org.ulcompsoc.ld30.components.Position;
47
import uk.org.ulcompsoc.ld30.components.Renderable;
58
import uk.org.ulcompsoc.ld30.components.Renderable.Shape;
69
import uk.org.ulcompsoc.ld30.components.Solid;
710
import uk.org.ulcompsoc.ld30.components.Velocity;
11+
import uk.org.ulcompsoc.ld30.systems.BallBoundsSystem;
12+
import uk.org.ulcompsoc.ld30.systems.BallCollisionSystem;
13+
import uk.org.ulcompsoc.ld30.systems.CheatingAISystem;
14+
import uk.org.ulcompsoc.ld30.systems.MouseTrackingSystem;
815
import uk.org.ulcompsoc.ld30.systems.MovementSystem;
916
import uk.org.ulcompsoc.ld30.systems.RenderingSystem;
1017

18+
import com.badlogic.ashley.core.ComponentMapper;
1119
import com.badlogic.ashley.core.Engine;
1220
import com.badlogic.ashley.core.Entity;
21+
import com.badlogic.gdx.Application;
1322
import com.badlogic.gdx.ApplicationAdapter;
1423
import com.badlogic.gdx.Gdx;
1524
import com.badlogic.gdx.graphics.Color;
1625
import com.badlogic.gdx.graphics.GL20;
1726
import com.badlogic.gdx.graphics.OrthographicCamera;
1827
import com.badlogic.gdx.math.Rectangle;
28+
import com.badlogic.gdx.math.Vector2;
1929

2030
public class LD30Test extends ApplicationAdapter {
21-
Engine engine = null;
22-
Entity rect = null;
23-
Entity circ = null;
31+
public enum VictoryType {
32+
RED, BLUE, NONE;
33+
}
34+
35+
public static VictoryType victoryType = VictoryType.NONE;
36+
37+
private Engine engine = null;
38+
private Entity playerBat = null;
39+
private Entity aiBat = null;
40+
private Entity ball = null;
2441

25-
OrthographicCamera camera = null;
42+
private OrthographicCamera camera = null;
2643

2744
@Override
2845
public void create() {
46+
Gdx.app.setLogLevel(Application.LOG_DEBUG);
2947
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
3048
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
3149

3250
engine = new Engine();
33-
rect = new Entity();
34-
circ = new Entity();
35-
36-
rect.add(new Position(5.0f, 10.0f));
37-
Renderable r1 = new Renderable();
38-
r1.color = Color.CYAN;
39-
r1.shape = Shape.RECTANGLE;
40-
rect.add(r1);
41-
rect.add(new Solid());
51+
ball = makeBall();
4252

43-
circ.add(new Position(30.0f, 30.0f));
44-
Renderable r2 = new Renderable();
45-
r2.color = Color.MAROON;
46-
r2.shape = Shape.CIRCLE;
47-
circ.add(r2);
48-
circ.add(new Solid());
49-
circ.add(new Velocity(10.0f, 10.0f));
53+
playerBat = makeBat(10.0f, 5.0f, Color.CYAN, false);
54+
aiBat = makeBat(Gdx.graphics.getWidth() - 5.0f - 8.0f, 5.0f, Color.RED, true);
5055

51-
engine.addEntity(rect);
52-
engine.addEntity(circ);
56+
engine.addEntity(ball);
57+
engine.addEntity(playerBat);
58+
engine.addEntity(aiBat);
5359

60+
engine.addSystem(new MouseTrackingSystem(camera, 0));
61+
engine.addSystem(new CheatingAISystem(1));
62+
engine.addSystem(new BallCollisionSystem(2));
5463
engine.addSystem(new MovementSystem(
55-
new Rectangle(0.0f, 0.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), 0));
64+
new Rectangle(0.0f, 0.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), 5));
5665
engine.addSystem(new RenderingSystem(camera, 10));
66+
engine.addSystem(new BallBoundsSystem(50));
5767
}
5868

5969
@Override
@@ -64,5 +74,57 @@ public void render() {
6474

6575
camera.update();
6676
engine.update(deltaTime);
77+
78+
if (!victoryType.equals(VictoryType.NONE)) {
79+
String victoryMessage = "The " + (victoryType.equals(VictoryType.BLUE) ? "blue" : "red") + " player wins!";
80+
81+
Gdx.app.log("VICTORY", victoryMessage);
82+
LD30Test.victoryType = VictoryType.NONE;
83+
84+
try {
85+
Thread.sleep(1000);
86+
} catch (InterruptedException e) {
87+
}
88+
89+
ComponentMapper.getFor(Position.class).get(ball).reset();
90+
Vector2 vel = ComponentMapper.getFor(Velocity.class).get(ball).velocity;
91+
vel.x = -vel.x;
92+
}
93+
}
94+
95+
public Entity makeBat(float x, float y, Color color, boolean isAI) {
96+
Entity e = new Entity();
97+
e.add(new Position(x, y));
98+
e.add(new Velocity());
99+
Renderable r1 = new Renderable();
100+
r1.color = color;
101+
r1.shape = Shape.RECTANGLE;
102+
r1.dimension.set(8.0f, 128.0f);
103+
e.add(r1);
104+
e.add(new Solid(10.0f));
105+
106+
if (isAI) {
107+
e.add(new CheatingAI(ball));
108+
} else {
109+
e.add(new MouseTracker(3.0f));
110+
}
111+
112+
return e;
113+
}
114+
115+
public Entity makeBall() {
116+
Entity e = new Entity();
117+
118+
e.add(new Position((float) Gdx.graphics.getWidth() / 2.0f, (float) Gdx.graphics.getHeight() / 2.0f));
119+
Renderable r2 = new Renderable();
120+
r2.color = Color.YELLOW;
121+
r2.shape = Shape.CIRCLE;
122+
r2.dimension.set(8.0f, 0.0f);
123+
e.add(r2);
124+
e.add(new Solid(1.0f));
125+
e.add(new Velocity(10.0f, 10.0f));
126+
e.add(new Ball());
127+
128+
return e;
67129
}
68130
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package uk.org.ulcompsoc.ld30.components;
2+
3+
import com.badlogic.ashley.core.Component;
4+
5+
/**
6+
* @author Ashley Davis (SgtCoDFish)
7+
*/
8+
public class Ball extends Component {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package uk.org.ulcompsoc.ld30.components;
2+
3+
import com.badlogic.ashley.core.Component;
4+
import com.badlogic.ashley.core.ComponentMapper;
5+
import com.badlogic.ashley.core.Entity;
6+
import com.badlogic.gdx.utils.GdxRuntimeException;
7+
8+
/**
9+
* @author Ashley Davis (SgtCoDFish)
10+
*/
11+
public class CheatingAI extends Component {
12+
public Entity followMe = null;
13+
14+
public CheatingAI(Entity followMe) {
15+
if (!ComponentMapper.getFor(Position.class).has(followMe)) {
16+
throw new GdxRuntimeException(
17+
"Created cheating ai with invalid ball: must have Position component already added.");
18+
}
19+
20+
this.followMe = followMe;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package uk.org.ulcompsoc.ld30.components;
2+
3+
import com.badlogic.ashley.core.Component;
4+
5+
/**
6+
* @author Ashley Davis (SgtCoDFish)
7+
*/
8+
public class MouseTracker extends Component {
9+
public float chaseRate = 1.0f;
10+
11+
public MouseTracker() {
12+
this(1.0f);
13+
}
14+
15+
public MouseTracker(float chaseRate) {
16+
this.chaseRate = chaseRate;
17+
}
18+
}

core/src/uk/org/ulcompsoc/ld30/components/Position.java

+9
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@
99
public class Position extends Component {
1010
public Vector2 position = null;
1111

12+
public float origX = 0.0f;
13+
public float origY = 0.0f;
14+
1215
public Position() {
1316
this(0.0f, 0.0f);
1417
}
1518

1619
public Position(float x, float y) {
1720
position = new Vector2(x, y);
21+
this.origX = x;
22+
this.origY = y;
23+
}
24+
25+
public void reset() {
26+
position.set(origX, origY);
1827
}
1928

2029
}

core/src/uk/org/ulcompsoc/ld30/components/Renderable.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.badlogic.ashley.core.Component;
44
import com.badlogic.gdx.graphics.Color;
5+
import com.badlogic.gdx.math.Vector2;
56

67
/**
78
* @author Ashley Davis (SgtCoDFish)
@@ -11,11 +12,19 @@ public static enum Shape {
1112
RECTANGLE, CIRCLE
1213
}
1314

14-
public Color color;
15-
public Shape shape;
15+
public Color color = null;
16+
public Shape shape = null;
17+
18+
// x = width, y = height for rect, x = radius for circle
19+
public Vector2 dimension = null;
1620

1721
public Renderable() {
1822
color = Color.WHITE;
1923
shape = Shape.RECTANGLE;
24+
dimension = new Vector2(8.0f, 8.0f);
25+
}
26+
27+
public static float getHeightFor(Renderable r) {
28+
return (r.shape == Shape.RECTANGLE ? r.dimension.y : (r.shape == Shape.CIRCLE ? r.dimension.x : -1.0f));
2029
}
2130
}

core/src/uk/org/ulcompsoc/ld30/components/Solid.java

+6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
* @author Ashley Davis (SgtCoDFish)
77
*/
88
public class Solid extends Component {
9+
public float mass = 10.0f;
910

1011
public Solid() {
12+
this(10.0f);
13+
}
14+
15+
public Solid(float mass) {
16+
this.mass = mass;
1117
}
1218

1319
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package uk.org.ulcompsoc.ld30.systems;
2+
3+
import uk.org.ulcompsoc.ld30.LD30Test;
4+
import uk.org.ulcompsoc.ld30.LD30Test.VictoryType;
5+
import uk.org.ulcompsoc.ld30.components.Ball;
6+
import uk.org.ulcompsoc.ld30.components.Position;
7+
import uk.org.ulcompsoc.ld30.components.Renderable;
8+
9+
import com.badlogic.ashley.core.ComponentMapper;
10+
import com.badlogic.ashley.core.Entity;
11+
import com.badlogic.ashley.core.Family;
12+
import com.badlogic.ashley.systems.IteratingSystem;
13+
import com.badlogic.gdx.Gdx;
14+
import com.badlogic.gdx.math.Vector2;
15+
16+
/**
17+
* @author Ashley Davis (SgtCoDFish)
18+
*/
19+
public class BallBoundsSystem extends IteratingSystem {
20+
private ComponentMapper<Position> posMapper = null;
21+
private ComponentMapper<Renderable> renderMapper = null;
22+
23+
@SuppressWarnings("unchecked")
24+
public BallBoundsSystem(int priority) {
25+
this(Family.getFor(Position.class, Renderable.class, Ball.class), priority);
26+
}
27+
28+
protected BallBoundsSystem(Family family, int priority) {
29+
super(family, priority);
30+
31+
posMapper = ComponentMapper.getFor(Position.class);
32+
renderMapper = ComponentMapper.getFor(Renderable.class);
33+
}
34+
35+
@Override
36+
public void processEntity(Entity entity, float deltaTime) {
37+
Vector2 pos = posMapper.get(entity).position.cpy();
38+
float radius = renderMapper.get(entity).dimension.x;
39+
pos.add(radius / 2.0f, radius / 2.0f);
40+
41+
if (pos.x < 0.0f) {
42+
LD30Test.victoryType = VictoryType.RED;
43+
} else if (pos.x > Gdx.graphics.getWidth()) {
44+
LD30Test.victoryType = VictoryType.BLUE;
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)