Skip to content

Commit

Permalink
fixed running too fast, jumping then jittering up and down. Had to di…
Browse files Browse the repository at this point in the history
…sable the collider for a short period of time.
  • Loading branch information
asalga committed Nov 5, 2014
1 parent 1a9847d commit 9bd50dd
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 31 deletions.
25 changes: 21 additions & 4 deletions BoundingBoxComponent.pde
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
////////////////////////
// WrapAroundComponent
////////////////////////
/////////////////////////
// BoundingBoxComponent
/////////////////////////
class BoundingBoxComponent extends Component {

float x, y, w, h;
float xOffest, yOffset;
int mask;
int type;
HashMap<String, GameObject> colliders;
boolean collidable;

BoundingBoxComponent() {
super();
Expand All @@ -17,6 +18,7 @@ class BoundingBoxComponent extends Component {
xOffest = yOffset = 0;
mask = 0;
colliders = new HashMap<String, GameObject>();
collidable = true;
}

void awake() {
Expand Down Expand Up @@ -46,7 +48,14 @@ class BoundingBoxComponent extends Component {
pushStyle();
strokeWeight(1);
noFill();
stroke(255, 0, 0);

if(isCollisable()){
stroke(255, 0, 0);
}
else{
stroke(255, 255, 255);
}

rect(x, -y + yOffset*2, w, h);
popStyle();
}
Expand All @@ -59,6 +68,14 @@ class BoundingBoxComponent extends Component {
void onCollision(GameObject other) {
}

void setEnableCollisions(boolean b){
collidable = b;
}

boolean isCollisable(){
return collidable;
}

void onCollisionEnter(GameObject other) {
colliders.put("" + other.id, other);
}
Expand Down
6 changes: 4 additions & 2 deletions BoundingBoxYComponent.pde
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class BoundingBoxYComponent extends BoundingBoxComponent {

// STRUCTURE
else if(other.hasTag("structure")){
mario.hitStructureY(other);

PhysicsComponent phy = (PhysicsComponent)gameObject.getComponent("PhysicsComponent");

Expand All @@ -78,6 +77,9 @@ class BoundingBoxYComponent extends BoundingBoxComponent {

mario._isJumping = false;
}
else{
mario.hitStructureY(other);
}
}
}
}
}
4 changes: 4 additions & 0 deletions CollisionManager.pde
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class CollisionManager {
continue;
}

if(bb1.isCollisable() == false || bb2.isCollisable() == false){
continue;
}

// Check the masks
if ((bb1.type & bb2.mask) == 0) {
numCollisionTestsSkipped++;
Expand Down
74 changes: 49 additions & 25 deletions MarioControllerComponent.pde
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
//////////////////////////
class MarioControllerComponent extends Component {

final float walkForce = 20; //550
final float jumpForce = 650; //350
//BoundingBoxComponent boundingBox;
final float walkForce = 20;
final float jumpForce = 650;

PhysicsComponent physics;
AnimationComponent animation;
float jumpTimer = 0;

BoundingBoxComponent boundingBox;

boolean canWalk;

Expand All @@ -33,12 +35,22 @@ class MarioControllerComponent extends Component {
if(physics != null){
physics.setHasFriction(true);
}

boundingBox = (BoundingBoxComponent)gameObject.getComponent("BoundingBoxComponent");
}

void update(float dt) {
// TODO: fix
super.update(dt);

jumpTimer += dt;

// We want to re-enable the collider, but only after it left the
// possibility of touching any other colliders
if(jumpTimer > 0.04){
boundingBox.setEnableCollisions(true);
}

_isInvinsible = Keyboard.isKeyDown(KEY_I);

// We don't want the player to be able to walk after
Expand Down Expand Up @@ -70,25 +82,36 @@ class MarioControllerComponent extends Component {
}

void idle() {
animation.play("idle");
if(animation != null){
animation.play("idle");
}
}

void walkRight() {
physics.applyForce(walkForce, 0);
animation.setFlipX(false);
animation.play("walk");

if(animation != null){
animation.setFlipX(false);
animation.play("walk");
}
}

void walkLeft() {
physics.applyForce(-walkForce, 0);
animation.setFlipX(true);
animation.play("walk");

if(animation != null){
animation.setFlipX(true);
animation.play("walk");
}
}

void jump() {
// If the player is told to jump but they already are touching
// a structure at the top, ignore the call.
BoundingBoxComponent boundingBox = (BoundingBoxComponent)gameObject.getComponent("BoundingBoxComponent");
boundingBox.setEnableCollisions(false);
jumpTimer = 0.0;

HashMap<String, GameObject> colliders = boundingBox.colliders;
for(String key : colliders.keySet()){
GameObject go = colliders.get(key);
Expand All @@ -100,7 +123,10 @@ class MarioControllerComponent extends Component {
controller.hit(gameObject);

soundManager.playSound("jump");
animation.play("jump");

if(animation != null){
animation.play("jump");
}
}
return;
}
Expand All @@ -117,7 +143,11 @@ class MarioControllerComponent extends Component {
physics.setTouchingFloor(false);
physics.applyJumpForce(jumpForce);
soundManager.playSound("jump");
animation.play("jump");

if(animation!=null){
animation.play("jump");
}

_isJumping = true;

// assume we'll land on the floor.
Expand All @@ -136,7 +166,7 @@ class MarioControllerComponent extends Component {

// Not moving in either x or y direction
boolean isIdle() {
return Keyboard.isKeyDown(KEY_LEFT) == false &&
return Keyboard.isKeyDown(KEY_LEFT) == false &&
Keyboard.isKeyDown(KEY_RIGHT) == false &&
canJump();
}
Expand Down Expand Up @@ -171,24 +201,18 @@ class MarioControllerComponent extends Component {
phy.setTouchingFloor(false);
phy.velocity.y = 0;

animation.play("jump");
if(animation != null){
animation.play("jump");
}
}

void hitStructureY(GameObject structure){
// LANDED ON TOP
if(gameObject.position.y > structure.position.y){
dprintln("Landed on top");
_isJumping = false;
}
// PUNCHED
else{
if(getJumpState()){
dprintln("Punched strucure");
physics.setVelocityY(0);
if(getJumpState()){
dprintln("Punched strucure");
physics.setVelocityY(0);

StructureControllerComponent controller = (StructureControllerComponent)structure.getComponent("StructureControllerComponent");
controller.hit(gameObject);
}
StructureControllerComponent controller = (StructureControllerComponent)structure.getComponent("StructureControllerComponent");
controller.hit(gameObject);
}
}

Expand Down

0 comments on commit 9bd50dd

Please sign in to comment.