Skip to content

Instantly share code, notes, and snippets.

@KaeruCT
Created May 2, 2012 21:57
Show Gist options
  • Save KaeruCT/2580833 to your computer and use it in GitHub Desktop.
Save KaeruCT/2580833 to your computer and use it in GitHub Desktop.
bad platformer basic entity
package com.cascadegames.androidgame;
import android.util.Log;
import jgame.JGObject;
public abstract class GameObject extends JGObject {
AndroidGame g;
double gravity = 0.9;
boolean stuck = false;
boolean jumping = false;
boolean bouncing = false;
boolean airborne = false;
boolean left = false;
double jump = 0;
double maxxspeed = 6;
double maxyspeed = 8;
private double yacceleration = 0;
// mostly used for grid-snapping
private final double kk;
public GameObject(double x, double y, int colid, String gfxname) {
super(gfxname, true, x, y, colid, gfxname, 1.0, 1.0);
yacceleration = gravity;
airborne = true;
g = AndroidGame.g;
kk = 2*g.TILE_SIZE/3;
}
public void hit_bg(int tilecid) {
if ( check_collision_top() ) {
hit_bg_top();
} else if ( check_collision_bottom() ) {
hit_bg_bottom();
}
if ( check_collision_left() ) {
hit_bg_left();
} else if ( check_collision_right() ) {
hit_bg_right();
}
}
public boolean check_collision_bottom() {
return checkBGCollision(0, g.TILE_SIZE-kk) == Collision.TILE;
}
public boolean check_collision_top() {
return checkBGCollision(0, -(g.TILE_SIZE-kk)) == Collision.TILE;
}
public boolean check_collision_left() {
return checkBGCollision(-g.TILE_SIZE, 0) == Collision.TILE;
}
public boolean check_collision_right() {
return checkBGCollision(g.TILE_SIZE, 0) == Collision.TILE;
}
public void hit(JGObject obj) {
if (checkCollision(1,-3*xspeed,-3*yspeed)==0) {
// reverse direction
xspeed = -xspeed;
yspeed = -yspeed;
}
}
public void jump() {
if( !jumping && !check_collision_top() ) {
jumping = true;
yspeed = -jump;
}
}
public void hit_bg_bottom() {
snapToGrid(0, kk);
y -= 0.00000001;
airborne = false;
jumping = false;
yacceleration = 0;
yspeed = 0;
bouncing = false;
}
public void hit_bg_top() {
snapToGrid(0, kk);
y += 0.00000001;
airborne = true;
jumping = false;
yacceleration = 0;
yspeed = -yspeed/8;
bouncing = true;
}
public void hit_bg_left() {
snapToGrid(kk, 0.0);
x += 0.000000001;
xspeed = 0;
//bouncing = true;
}
public void hit_bg_right() {
snapToGrid(kk, 0.0);
x -= 0.000000001;
xspeed = 0;
//bouncing = true;
}
public void move() {
if(xspeed > maxxspeed) {
xspeed = maxxspeed;
}else if(xspeed < -maxxspeed){
xspeed = -maxxspeed;
}
super.move();
if(jumping && yspeed >= 0) {
jumping = false;
}
if(yspeed + yacceleration >= maxyspeed) {
yspeed = maxyspeed;
} else {
yspeed += yacceleration;
}
airborne = !check_collision_bottom();
if( airborne ) {
yacceleration = gravity;
jumping = true;
if ( !check_collision_left() && !check_collision_right()) {
bouncing = false;
}
} else if( !jumping ) {
hit_bg_bottom();
}
}
public abstract void paint();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment