Skip to content

Commit

Permalink
models/Entity, models/Player:
Browse files Browse the repository at this point in the history
- Added methods for defining the max absoulte velocity (x and y axes)
for Entities
- Added jump functionality
  • Loading branch information
larsbutler committed Sep 29, 2012
1 parent 37c7c07 commit a608da1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/com/larsbutler/gamedemo/models/Entity.java
Expand Up @@ -15,6 +15,8 @@ public abstract class Entity {
protected int width;
protected int height;

protected boolean canJump;

public Entity(double x, double y, int width, int height) {
xState = new State();
xState.p = x;
Expand Down Expand Up @@ -92,6 +94,14 @@ public void setHeight(int height) {
this.height = height;
}

public boolean canJump() {
return canJump;
}

public void setCanJump(boolean canJump) {
this.canJump = canJump;
}

public Rectangle2D rect() {
return new Rectangle2D.Double(xState.p, yState.p, (double)width, (double)height);
}
Expand All @@ -100,5 +110,15 @@ public Rectangle2D prevRect() {
return new Rectangle2D.Double(prevXState.p, prevYState.p, (double)width, (double)height);
}

public double getMaxAbsXVel() {
return Double.POSITIVE_INFINITY;
}

public double getMaxAbsYVel() {
return Double.POSITIVE_INFINITY;
}

public abstract void jump();

public abstract void render(Graphics g, double alpha);
}
20 changes: 19 additions & 1 deletion src/com/larsbutler/gamedemo/models/Player.java
Expand Up @@ -3,10 +3,16 @@
import java.awt.Color;
import java.awt.Graphics;

import com.larsbutler.gamedemo.math.MathUtil;

public class Player extends Entity {

public static double MAX_XVEL_PER_UPDATE = 500;
public static double CEL_PER_SECOND = 2000; // accel/decel per second
public static double CEL_PER_SECOND = 1000; // accel/decel per second
/**
* Jump y-axis displacement.
*/
public static double JUMP_HEIGHT = 200.0;

private boolean left;
private boolean right;
Expand All @@ -31,6 +37,18 @@ public void setRight(boolean right) {
this.right = right;
}

public void jump() {
if (canJump) {
this.getYState().v = -MathUtil.u(-MathUtil.GRAVITY, JUMP_HEIGHT, 0.0);
canJump = false;
}
}

@Override
public double getMaxAbsXVel() {
return MAX_XVEL_PER_UPDATE;
}

public void render(Graphics g, double alpha) {
g.setColor(Color.orange);
g.fillRect(
Expand Down

0 comments on commit a608da1

Please sign in to comment.