Permalink
Browse files

Added Tree, Island can be reset

Pressing R will rebuild the island which is useful for testing. Trees are now added at random. Cleaned up methods no longer needed and added new to deal with generating the random trees.
  • Loading branch information...
tyler6699 committed Aug 6, 2017
1 parent 723f333 commit 569c7b84583878b25b04141841b4d8b80b5e7d29
View
Binary file not shown.
@@ -27,6 +27,7 @@
// DEBUG
public boolean debug;
public boolean reset;
// SCREEN
int screenWidth;
@@ -114,6 +115,9 @@ public boolean keyUp(int keycode) {
case Keys.BACKSPACE:
debug = !debug;
break;
case Keys.R:
reset = true;
break;
}
return false;
}
@@ -9,7 +9,8 @@
}
public enum EntityType {
HERO
HERO,
TREE
}
}
@@ -15,6 +15,9 @@
// HERO
public static Texture hero;
// Entity
public static Texture tree;
public static void load_assets(){
// HERO
hero = new Texture("entities/hero/hero.png");
@@ -40,8 +43,9 @@ public static void load_assets(){
water02 = new Texture("8x8/water/water_02.png");
water03 = new Texture("8x8/water/water_03.png");
water04 = new Texture("8x8/water/water_04.png");
cliff = new Texture(Gdx.files.internal("8x8/cliff.png"));
cliff = new Texture(Gdx.files.internal("8x8/cliff.png"));
tree = new Texture("entities/tree.png");
}
public void dispose(){
@@ -61,5 +65,6 @@ public void dispose(){
water03.dispose();
water04.dispose();
cliff.dispose();
tree.dispose();
}
}
@@ -9,10 +9,10 @@
public class Box2DHelper {
public static Body createBody(World world, float width, float height, Vector3 pos, BodyDef.BodyType type) {
public static Body createBody(World world, float width, float height, float xOffset, float yOffset, Vector3 pos, BodyDef.BodyType type) {
Body body;
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(pos.x + width/2, pos.y + height/2);
bodyDef.position.set( (pos.x + width/2) + xOffset, (pos.y + height/2) + yOffset);
bodyDef.angle = 0;
bodyDef.fixedRotation = true;
bodyDef.type = type;
@@ -1,11 +1,14 @@
package uk.co.carelesslabs.box2d;
import uk.co.carelesslabs.Control;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;
public class Box2DWorld {
public World world;
@@ -22,5 +25,13 @@ public void tick(OrthographicCamera camera, Control control){
world.step(Gdx.app.getGraphics().getDeltaTime(), 6, 2);
world.clearForces();
}
public void clearAllBodies() {
Array<Body> bodies = new Array<Body>();
world.getBodies(bodies);
for(Body b: bodies){
world.destroyBody(b);
}
}
}
@@ -1,10 +1,12 @@
package uk.co.carelesslabs;
import java.util.ArrayList;
import java.util.Collections;
import uk.co.carelesslabs.box2d.Box2DWorld;
import uk.co.carelesslabs.map.Tile;
import uk.co.carelesslabs.map.Island;
import uk.co.carelesslans.entity.Entity;
import uk.co.carelesslans.entity.Hero;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
@@ -55,6 +57,7 @@ public void create() {
// Hero
hero = new Hero(island.centreTile.pos, box2D);
island.entities.add(hero);
}
@Override
@@ -63,10 +66,18 @@ public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// GAME LOGIC
if(control.reset){
island.reset(box2D);
hero.reset(box2D,island.getCentrePosition());
island.entities.add(hero);
control.reset = false;
}
hero.update(control);
camera.position.lerp(hero.pos, .1f);
camera.update();
Collections.sort(island.entities);
// GAME DRAW
batch.setProjectionMatrix(camera.combined);
@@ -80,7 +91,11 @@ public void render () {
if (tile.secondaryTexture != null) batch.draw(tile.secondaryTexture, tile.pos.x, tile.pos.y, tile.size, tile.size);
}
}
hero.draw(batch);
// Draw all entities
for(Entity e: island.entities){
e.draw(batch);
}
batch.end();
box2D.tick(camera, control);
@@ -3,12 +3,14 @@
import java.util.ArrayList;
import java.util.Arrays;
import uk.co.carelesslabs.Enums.TileType;
import uk.co.carelesslabs.Entity;
import uk.co.carelesslabs.Media;
import uk.co.carelesslabs.box2d.Box2DHelper;
import uk.co.carelesslabs.box2d.Box2DWorld;
import uk.co.carelesslans.entity.Entity;
import uk.co.carelesslans.entity.Tree;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
public class Island {
@@ -20,7 +22,7 @@
// ONE CHUNK
public Chunk chunk;
ArrayList<Entity> entities = new ArrayList<Entity>();
public ArrayList<Entity> entities = new ArrayList<Entity>();
// TRACK CLICK
int currentTileNo;
@@ -37,16 +39,23 @@
String[] aGrassTopLeft = {"000000001"};
public Island(Box2DWorld box2D){
reset(box2D);
}
public void reset(Box2DWorld box2D) {
entities.clear();
box2D.clearAllBodies();
setupTiles();
codeTiles();
generateHitboxes(box2D);
addEntities(box2D);
}
private void generateHitboxes(Box2DWorld box2D) {
for(ArrayList<Tile> row : chunk.tiles){
for(Tile tile : row){
if(tile.isNotPassable() && tile.notIsAllWater()){
Box2DHelper.createBody(box2D.world, chunk.tileSize, chunk.tileSize, tile.pos, BodyType.StaticBody);
Box2DHelper.createBody(box2D.world, chunk.tileSize, chunk.tileSize, 0, 0, tile.pos, BodyType.StaticBody);
}
}
}
@@ -205,6 +214,23 @@ private void codeTiles() {
}
}
}
private void addEntities(Box2DWorld box2D) {
// Loop all tiles and add random trees
for(ArrayList<Tile> row : chunk.tiles){
for(Tile tile : row){
if (tile.isGrass()){
if(MathUtils.random(100) > 90){
entities.add(new Tree(tile.pos, box2D));
}
}
}
}
}
public Vector3 getCentrePosition(){
return centreTile.pos;
}
public void dispose() {
@@ -1,7 +1,7 @@
package uk.co.carelesslabs.map;
import uk.co.carelesslabs.Enums.TileType;
import uk.co.carelesslabs.Entity;
import uk.co.carelesslans.entity.Entity;
import com.badlogic.gdx.graphics.Texture;
public class Tile extends Entity {
@@ -1,12 +1,12 @@
package uk.co.carelesslabs;
package uk.co.carelesslans.entity;
import uk.co.carelesslabs.Enums.EntityType;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
public class Entity {
public class Entity implements Comparable<Entity> {
public Vector3 pos;
public Texture texture;
public float width;
@@ -25,5 +25,13 @@ public Entity(){
public void draw(SpriteBatch batch){
batch.draw(texture, pos.x, pos.y, width, height);
}
@Override
public int compareTo(Entity e) {
float temp_y = e.pos.y;
float compare_y = pos.y;
return (temp_y < compare_y ) ? -1: (temp_y > compare_y) ? 1:0 ;
}
}
@@ -1,7 +1,9 @@
package uk.co.carelesslabs;
package uk.co.carelesslans.entity;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import uk.co.carelesslabs.Control;
import uk.co.carelesslabs.Media;
import uk.co.carelesslabs.Enums.EntityType;
import uk.co.carelesslabs.box2d.Box2DHelper;
import uk.co.carelesslabs.box2d.Box2DWorld;
@@ -12,11 +14,14 @@ public Hero(Vector3 pos, Box2DWorld box2d){
type = EntityType.HERO;
width = 8;
height = 8;
this.pos.x = pos.x;
this.pos.y = pos.y;
texture = Media.hero;
speed = 30;
body = Box2DHelper.createBody(box2d.world, width, height/2, pos, BodyType.DynamicBody);
reset(box2d, pos);
}
public void reset(Box2DWorld box2d, Vector3 pos) {
this.pos.set(pos);
body = Box2DHelper.createBody(box2d.world, width/2, height/2, width/4, 0, pos, BodyType.DynamicBody);
}
public void update(Control control) {
@@ -32,13 +37,5 @@ public void update(Control control) {
pos.x = body.getPosition().x - width/2;
pos.y = body.getPosition().y - height/4;
}
public float getCameraX() {
return pos.x + width/2;
}
public float getCameraY() {
return pos.y + height/2;
}
}
@@ -0,0 +1,22 @@
package uk.co.carelesslans.entity;
import uk.co.carelesslabs.Enums.EntityType;
import uk.co.carelesslabs.Media;
import uk.co.carelesslabs.box2d.Box2DHelper;
import uk.co.carelesslabs.box2d.Box2DWorld;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.BodyDef;
public class Tree extends Entity{
public Tree(Vector3 pos, Box2DWorld box2d){
super();
type = EntityType.TREE;
width = 8;
height = 8;
this.pos = pos;
texture = Media.tree;
body = Box2DHelper.createBody(box2d.world, width/2, height/2, width/4, 0, pos, BodyDef.BodyType.StaticBody);
}
}
View
Binary file not shown.

0 comments on commit 569c7b8

Please sign in to comment.