Skip to content

Commit

Permalink
Complete pong implementation :D
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtCoDFish committed Aug 22, 2014
1 parent 4510c71 commit 6651bf2
Show file tree
Hide file tree
Showing 13 changed files with 416 additions and 31 deletions.
108 changes: 85 additions & 23 deletions core/src/uk/org/ulcompsoc/ld30/LD30Test.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,69 @@
package uk.org.ulcompsoc.ld30;

import uk.org.ulcompsoc.ld30.components.Ball;
import uk.org.ulcompsoc.ld30.components.CheatingAI;
import uk.org.ulcompsoc.ld30.components.MouseTracker;
import uk.org.ulcompsoc.ld30.components.Position;
import uk.org.ulcompsoc.ld30.components.Renderable;
import uk.org.ulcompsoc.ld30.components.Renderable.Shape;
import uk.org.ulcompsoc.ld30.components.Solid;
import uk.org.ulcompsoc.ld30.components.Velocity;
import uk.org.ulcompsoc.ld30.systems.BallBoundsSystem;
import uk.org.ulcompsoc.ld30.systems.BallCollisionSystem;
import uk.org.ulcompsoc.ld30.systems.CheatingAISystem;
import uk.org.ulcompsoc.ld30.systems.MouseTrackingSystem;
import uk.org.ulcompsoc.ld30.systems.MovementSystem;
import uk.org.ulcompsoc.ld30.systems.RenderingSystem;

import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;

public class LD30Test extends ApplicationAdapter {
Engine engine = null;
Entity rect = null;
Entity circ = null;
public enum VictoryType {
RED, BLUE, NONE;
}

public static VictoryType victoryType = VictoryType.NONE;

private Engine engine = null;
private Entity playerBat = null;
private Entity aiBat = null;
private Entity ball = null;

OrthographicCamera camera = null;
private OrthographicCamera camera = null;

@Override
public void create() {
Gdx.app.setLogLevel(Application.LOG_DEBUG);
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

engine = new Engine();
rect = new Entity();
circ = new Entity();

rect.add(new Position(5.0f, 10.0f));
Renderable r1 = new Renderable();
r1.color = Color.CYAN;
r1.shape = Shape.RECTANGLE;
rect.add(r1);
rect.add(new Solid());
ball = makeBall();

circ.add(new Position(30.0f, 30.0f));
Renderable r2 = new Renderable();
r2.color = Color.MAROON;
r2.shape = Shape.CIRCLE;
circ.add(r2);
circ.add(new Solid());
circ.add(new Velocity(10.0f, 10.0f));
playerBat = makeBat(10.0f, 5.0f, Color.CYAN, false);
aiBat = makeBat(Gdx.graphics.getWidth() - 5.0f - 8.0f, 5.0f, Color.RED, true);

engine.addEntity(rect);
engine.addEntity(circ);
engine.addEntity(ball);
engine.addEntity(playerBat);
engine.addEntity(aiBat);

engine.addSystem(new MouseTrackingSystem(camera, 0));
engine.addSystem(new CheatingAISystem(1));
engine.addSystem(new BallCollisionSystem(2));
engine.addSystem(new MovementSystem(
new Rectangle(0.0f, 0.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), 0));
new Rectangle(0.0f, 0.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()), 5));
engine.addSystem(new RenderingSystem(camera, 10));
engine.addSystem(new BallBoundsSystem(50));
}

@Override
Expand All @@ -64,5 +74,57 @@ public void render() {

camera.update();
engine.update(deltaTime);

if (!victoryType.equals(VictoryType.NONE)) {
String victoryMessage = "The " + (victoryType.equals(VictoryType.BLUE) ? "blue" : "red") + " player wins!";

Gdx.app.log("VICTORY", victoryMessage);
LD30Test.victoryType = VictoryType.NONE;

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}

ComponentMapper.getFor(Position.class).get(ball).reset();
Vector2 vel = ComponentMapper.getFor(Velocity.class).get(ball).velocity;
vel.x = -vel.x;
}
}

public Entity makeBat(float x, float y, Color color, boolean isAI) {
Entity e = new Entity();
e.add(new Position(x, y));
e.add(new Velocity());
Renderable r1 = new Renderable();
r1.color = color;
r1.shape = Shape.RECTANGLE;
r1.dimension.set(8.0f, 128.0f);
e.add(r1);
e.add(new Solid(10.0f));

if (isAI) {
e.add(new CheatingAI(ball));
} else {
e.add(new MouseTracker(3.0f));
}

return e;
}

public Entity makeBall() {
Entity e = new Entity();

e.add(new Position((float) Gdx.graphics.getWidth() / 2.0f, (float) Gdx.graphics.getHeight() / 2.0f));
Renderable r2 = new Renderable();
r2.color = Color.YELLOW;
r2.shape = Shape.CIRCLE;
r2.dimension.set(8.0f, 0.0f);
e.add(r2);
e.add(new Solid(1.0f));
e.add(new Velocity(10.0f, 10.0f));
e.add(new Ball());

return e;
}
}
10 changes: 10 additions & 0 deletions core/src/uk/org/ulcompsoc/ld30/components/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package uk.org.ulcompsoc.ld30.components;

import com.badlogic.ashley.core.Component;

/**
* @author Ashley Davis (SgtCoDFish)
*/
public class Ball extends Component {

}
22 changes: 22 additions & 0 deletions core/src/uk/org/ulcompsoc/ld30/components/CheatingAI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.org.ulcompsoc.ld30.components;

import com.badlogic.ashley.core.Component;
import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.utils.GdxRuntimeException;

/**
* @author Ashley Davis (SgtCoDFish)
*/
public class CheatingAI extends Component {
public Entity followMe = null;

public CheatingAI(Entity followMe) {
if (!ComponentMapper.getFor(Position.class).has(followMe)) {
throw new GdxRuntimeException(
"Created cheating ai with invalid ball: must have Position component already added.");
}

this.followMe = followMe;
}
}
18 changes: 18 additions & 0 deletions core/src/uk/org/ulcompsoc/ld30/components/MouseTracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package uk.org.ulcompsoc.ld30.components;

import com.badlogic.ashley.core.Component;

/**
* @author Ashley Davis (SgtCoDFish)
*/
public class MouseTracker extends Component {
public float chaseRate = 1.0f;

public MouseTracker() {
this(1.0f);
}

public MouseTracker(float chaseRate) {
this.chaseRate = chaseRate;
}
}
9 changes: 9 additions & 0 deletions core/src/uk/org/ulcompsoc/ld30/components/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@
public class Position extends Component {
public Vector2 position = null;

public float origX = 0.0f;
public float origY = 0.0f;

public Position() {
this(0.0f, 0.0f);
}

public Position(float x, float y) {
position = new Vector2(x, y);
this.origX = x;
this.origY = y;
}

public void reset() {
position.set(origX, origY);
}

}
13 changes: 11 additions & 2 deletions core/src/uk/org/ulcompsoc/ld30/components/Renderable.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.badlogic.ashley.core.Component;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;

/**
* @author Ashley Davis (SgtCoDFish)
Expand All @@ -11,11 +12,19 @@ public static enum Shape {
RECTANGLE, CIRCLE
}

public Color color;
public Shape shape;
public Color color = null;
public Shape shape = null;

// x = width, y = height for rect, x = radius for circle
public Vector2 dimension = null;

public Renderable() {
color = Color.WHITE;
shape = Shape.RECTANGLE;
dimension = new Vector2(8.0f, 8.0f);
}

public static float getHeightFor(Renderable r) {
return (r.shape == Shape.RECTANGLE ? r.dimension.y : (r.shape == Shape.CIRCLE ? r.dimension.x : -1.0f));
}
}
6 changes: 6 additions & 0 deletions core/src/uk/org/ulcompsoc/ld30/components/Solid.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
* @author Ashley Davis (SgtCoDFish)
*/
public class Solid extends Component {
public float mass = 10.0f;

public Solid() {
this(10.0f);
}

public Solid(float mass) {
this.mass = mass;
}

}
47 changes: 47 additions & 0 deletions core/src/uk/org/ulcompsoc/ld30/systems/BallBoundsSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package uk.org.ulcompsoc.ld30.systems;

import uk.org.ulcompsoc.ld30.LD30Test;
import uk.org.ulcompsoc.ld30.LD30Test.VictoryType;
import uk.org.ulcompsoc.ld30.components.Ball;
import uk.org.ulcompsoc.ld30.components.Position;
import uk.org.ulcompsoc.ld30.components.Renderable;

import com.badlogic.ashley.core.ComponentMapper;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;

/**
* @author Ashley Davis (SgtCoDFish)
*/
public class BallBoundsSystem extends IteratingSystem {
private ComponentMapper<Position> posMapper = null;
private ComponentMapper<Renderable> renderMapper = null;

@SuppressWarnings("unchecked")
public BallBoundsSystem(int priority) {
this(Family.getFor(Position.class, Renderable.class, Ball.class), priority);
}

protected BallBoundsSystem(Family family, int priority) {
super(family, priority);

posMapper = ComponentMapper.getFor(Position.class);
renderMapper = ComponentMapper.getFor(Renderable.class);
}

@Override
public void processEntity(Entity entity, float deltaTime) {
Vector2 pos = posMapper.get(entity).position.cpy();
float radius = renderMapper.get(entity).dimension.x;
pos.add(radius / 2.0f, radius / 2.0f);

if (pos.x < 0.0f) {
LD30Test.victoryType = VictoryType.RED;
} else if (pos.x > Gdx.graphics.getWidth()) {
LD30Test.victoryType = VictoryType.BLUE;
}
}
}
Loading

0 comments on commit 6651bf2

Please sign in to comment.