Created
September 16, 2016 01:54
-
-
Save DCubix/2ef0ae2843ed1d3e1e8ab61c956a24e5 to your computer and use it in GitHub Desktop.
Simple jumping box using LKGE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package logikers.lkge.test; | |
import logikers.lkge.components.*; | |
import logikers.lkge.core.*; | |
import logikers.lkge.math.Rect; | |
import logikers.lkge.math.Vector2; | |
import logikers.lkge.physics.Collision; | |
import logikers.lkge.physics.PhysicsType; | |
import logikers.lkge.physics.Polygon; | |
import logikers.lkge.scenegraph.Entity; | |
import logikers.lkge.scenegraph.Scene; | |
class PlayerController extends Physics { | |
public boolean grounded = false; | |
public float speed = 10; | |
public PlayerController() {} | |
@Override | |
public void init() { | |
super.init(); | |
setPhysicsType(PhysicsType.DYNAMIC); | |
} | |
@Override | |
public void update(float dt) { | |
if (Input.getKey(Input.KEY_LEFT)) { | |
applyForce(new Vector2(-1, 0).mul(speed * 100).mul(dt)); | |
} else if (Input.getKey(Input.KEY_RIGHT)) { | |
applyForce(new Vector2(1, 0).mul(speed * 100).mul(dt)); | |
} | |
if (grounded) { | |
if (Input.getKeyDown(Input.KEY_SPACE)) { | |
applyImpulse(new Vector2(0, -1).mul(speed), new Vector2()); | |
grounded = false; | |
} | |
} | |
super.update(dt); | |
} | |
@Override | |
public void collisionEnter(Collision c) { | |
if (c.hitEntity != null) { | |
if (c.hitEntity.getTag() == "GROUND") { | |
grounded = true; | |
} | |
} | |
} | |
} | |
public class Test { | |
public static void main(String[] args) throws InterruptedException { | |
Game g = new Game(); | |
Scene sce = g.getCurrentScene(); | |
Entity box = sce.createEntity(-1); | |
box.setTag("BOX"); | |
Entity camera = sce.createEntity(-1); | |
Camera cam = new Camera(); | |
cam.setZoom(1); | |
camera.addComponent(cam); | |
g.setAssetManagerCallback(new AssetManagerCallback() { | |
@Override | |
public void preload(AssetManager assets) { | |
assets.addTexture("box", "res://logikers/lkge/resources/box.jpg"); | |
} | |
@Override | |
public void finished(AssetManager assets) { | |
SpriteRenderer spriteRen = new SpriteRenderer(assets.getTexture("box")); | |
spriteRen.getOrigin().set(0.5f, 0.5f); | |
box.addComponent(spriteRen); | |
CollisionShape box_s = new CollisionShape(CollisionShape.ShapeType.POLYGON); | |
box_s.setPolygon(Polygon.createRect(new Rect(-32, -32, 64, 64))); | |
box.addComponent(box_s); | |
box.addComponent(new PlayerController()); | |
box.setPosition(new Vector2(0, -200)); | |
Entity floor = sce.createEntity(-1); | |
floor.setTag("GROUND"); | |
CollisionShape floor_s = new CollisionShape(CollisionShape.ShapeType.POLYGON); | |
floor_s.setPolygon(Polygon.createRect(new Rect(-512, -10, 1024, 20))); | |
floor.addComponent(floor_s); | |
Physics sb = new Physics(); | |
floor.addComponent(sb); | |
floor.setPosition(new Vector2(0, 70)); | |
} | |
}); | |
App.setGame(g); | |
App.start("Testing", 640, 480); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment