Skip to content

Commit

Permalink
Added Tree, Island can be reset
Browse files Browse the repository at this point in the history
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 569c7b8
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 26 deletions.
Binary file added core/assets/entities/tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions core/src/uk/co/carelesslabs/Control.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Control extends InputAdapter implements InputProcessor {


// DEBUG // DEBUG
public boolean debug; public boolean debug;
public boolean reset;


// SCREEN // SCREEN
int screenWidth; int screenWidth;
Expand Down Expand Up @@ -114,6 +115,9 @@ public boolean keyUp(int keycode) {
case Keys.BACKSPACE: case Keys.BACKSPACE:
debug = !debug; debug = !debug;
break; break;
case Keys.R:
reset = true;
break;
} }
return false; return false;
} }
Expand Down
3 changes: 2 additions & 1 deletion core/src/uk/co/carelesslabs/Enums.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public enum TileType {
} }


public enum EntityType { public enum EntityType {
HERO HERO,
TREE
} }


} }
9 changes: 7 additions & 2 deletions core/src/uk/co/carelesslabs/Media.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class Media {
// HERO // HERO
public static Texture hero; public static Texture hero;


// Entity
public static Texture tree;

public static void load_assets(){ public static void load_assets(){
// HERO // HERO
hero = new Texture("entities/hero/hero.png"); hero = new Texture("entities/hero/hero.png");
Expand All @@ -40,8 +43,9 @@ public static void load_assets(){
water02 = new Texture("8x8/water/water_02.png"); water02 = new Texture("8x8/water/water_02.png");
water03 = new Texture("8x8/water/water_03.png"); water03 = new Texture("8x8/water/water_03.png");
water04 = new Texture("8x8/water/water_04.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(){ public void dispose(){
Expand All @@ -61,5 +65,6 @@ public void dispose(){
water03.dispose(); water03.dispose();
water04.dispose(); water04.dispose();
cliff.dispose(); cliff.dispose();
tree.dispose();
} }
} }
4 changes: 2 additions & 2 deletions core/src/uk/co/carelesslabs/box2d/Box2DHelper.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@


public class Box2DHelper { 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; Body body;
BodyDef bodyDef = new BodyDef(); 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.angle = 0;
bodyDef.fixedRotation = true; bodyDef.fixedRotation = true;
bodyDef.type = type; bodyDef.type = type;
Expand Down
11 changes: 11 additions & 0 deletions core/src/uk/co/carelesslabs/box2d/Box2DWorld.java
Original file line number Original file line Diff line number Diff line change
@@ -1,11 +1,14 @@
package uk.co.carelesslabs.box2d; package uk.co.carelesslabs.box2d;


import uk.co.carelesslabs.Control; import uk.co.carelesslabs.Control;

import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2; 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.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World; import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.utils.Array;


public class Box2DWorld { public class Box2DWorld {
public World world; public World world;
Expand All @@ -22,5 +25,13 @@ public void tick(OrthographicCamera camera, Control control){
world.step(Gdx.app.getGraphics().getDeltaTime(), 6, 2); world.step(Gdx.app.getGraphics().getDeltaTime(), 6, 2);
world.clearForces(); world.clearForces();
} }

public void clearAllBodies() {
Array<Body> bodies = new Array<Body>();
world.getBodies(bodies);
for(Body b: bodies){
world.destroyBody(b);
}
}


} }
19 changes: 17 additions & 2 deletions core/src/uk/co/carelesslabs/gameclass.java
Original file line number Original file line Diff line number Diff line change
@@ -1,10 +1,12 @@
package uk.co.carelesslabs; package uk.co.carelesslabs;


import java.util.ArrayList; import java.util.ArrayList;

import java.util.Collections;
import uk.co.carelesslabs.box2d.Box2DWorld; import uk.co.carelesslabs.box2d.Box2DWorld;
import uk.co.carelesslabs.map.Tile; import uk.co.carelesslabs.map.Tile;
import uk.co.carelesslabs.map.Island; 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.ApplicationAdapter;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
Expand Down Expand Up @@ -55,6 +57,7 @@ public void create() {


// Hero // Hero
hero = new Hero(island.centreTile.pos, box2D); hero = new Hero(island.centreTile.pos, box2D);
island.entities.add(hero);
} }


@Override @Override
Expand All @@ -63,10 +66,18 @@ public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


// GAME LOGIC // GAME LOGIC
if(control.reset){
island.reset(box2D);
hero.reset(box2D,island.getCentrePosition());
island.entities.add(hero);
control.reset = false;
}

hero.update(control); hero.update(control);


camera.position.lerp(hero.pos, .1f); camera.position.lerp(hero.pos, .1f);
camera.update(); camera.update();
Collections.sort(island.entities);


// GAME DRAW // GAME DRAW
batch.setProjectionMatrix(camera.combined); batch.setProjectionMatrix(camera.combined);
Expand All @@ -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); 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(); batch.end();


box2D.tick(camera, control); box2D.tick(camera, control);
Expand Down
32 changes: 29 additions & 3 deletions core/src/uk/co/carelesslabs/map/Island.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import uk.co.carelesslabs.Enums.TileType; import uk.co.carelesslabs.Enums.TileType;
import uk.co.carelesslabs.Entity;
import uk.co.carelesslabs.Media; import uk.co.carelesslabs.Media;
import uk.co.carelesslabs.box2d.Box2DHelper; import uk.co.carelesslabs.box2d.Box2DHelper;
import uk.co.carelesslabs.box2d.Box2DWorld; 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.graphics.Texture;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;


public class Island { public class Island {
Expand All @@ -20,7 +22,7 @@ public class Island {


// ONE CHUNK // ONE CHUNK
public Chunk chunk; public Chunk chunk;
ArrayList<Entity> entities = new ArrayList<Entity>(); public ArrayList<Entity> entities = new ArrayList<Entity>();


// TRACK CLICK // TRACK CLICK
int currentTileNo; int currentTileNo;
Expand All @@ -37,16 +39,23 @@ public class Island {
String[] aGrassTopLeft = {"000000001"}; String[] aGrassTopLeft = {"000000001"};


public Island(Box2DWorld box2D){ public Island(Box2DWorld box2D){
reset(box2D);
}

public void reset(Box2DWorld box2D) {
entities.clear();
box2D.clearAllBodies();
setupTiles(); setupTiles();
codeTiles(); codeTiles();
generateHitboxes(box2D); generateHitboxes(box2D);
addEntities(box2D);
} }


private void generateHitboxes(Box2DWorld box2D) { private void generateHitboxes(Box2DWorld box2D) {
for(ArrayList<Tile> row : chunk.tiles){ for(ArrayList<Tile> row : chunk.tiles){
for(Tile tile : row){ for(Tile tile : row){
if(tile.isNotPassable() && tile.notIsAllWater()){ 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);
} }
} }
} }
Expand Down Expand Up @@ -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() { public void dispose() {


Expand Down
2 changes: 1 addition & 1 deletion core/src/uk/co/carelesslabs/map/Tile.java
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@
package uk.co.carelesslabs.map; package uk.co.carelesslabs.map;


import uk.co.carelesslabs.Enums.TileType; import uk.co.carelesslabs.Enums.TileType;
import uk.co.carelesslabs.Entity; import uk.co.carelesslans.entity.Entity;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;


public class Tile extends Entity { public class Tile extends Entity {
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1,12 +1,12 @@
package uk.co.carelesslabs; package uk.co.carelesslans.entity;


import uk.co.carelesslabs.Enums.EntityType; import uk.co.carelesslabs.Enums.EntityType;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body; import com.badlogic.gdx.physics.box2d.Body;


public class Entity { public class Entity implements Comparable<Entity> {
public Vector3 pos; public Vector3 pos;
public Texture texture; public Texture texture;
public float width; public float width;
Expand All @@ -25,5 +25,13 @@ public Entity(){
public void draw(SpriteBatch batch){ public void draw(SpriteBatch batch){
batch.draw(texture, pos.x, pos.y, width, height); 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 ;
}


} }
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,9 @@
package uk.co.carelesslabs; package uk.co.carelesslans.entity;


import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType; 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.Enums.EntityType;
import uk.co.carelesslabs.box2d.Box2DHelper; import uk.co.carelesslabs.box2d.Box2DHelper;
import uk.co.carelesslabs.box2d.Box2DWorld; import uk.co.carelesslabs.box2d.Box2DWorld;
Expand All @@ -12,11 +14,14 @@ public Hero(Vector3 pos, Box2DWorld box2d){
type = EntityType.HERO; type = EntityType.HERO;
width = 8; width = 8;
height = 8; height = 8;
this.pos.x = pos.x;
this.pos.y = pos.y;
texture = Media.hero; texture = Media.hero;
speed = 30; 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) { public void update(Control control) {
Expand All @@ -32,13 +37,5 @@ public void update(Control control) {
pos.x = body.getPosition().x - width/2; pos.x = body.getPosition().x - width/2;
pos.y = body.getPosition().y - height/4; pos.y = body.getPosition().y - height/4;
} }


public float getCameraX() {
return pos.x + width/2;
}

public float getCameraY() {
return pos.y + height/2;
}

} }
22 changes: 22 additions & 0 deletions core/src/uk/co/carelesslans/entity/Tree.java
Original file line number Original file line Diff line number Diff line change
@@ -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);
}

}
Binary file added desktop/bin/entities/tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 569c7b8

Please sign in to comment.