Skip to content
Permalink
Browse files

Added simple bullet classes, spacebar to fire

Needs some code review but bullets fire, have a Box2D sensor and remove after traveling a given distance.
  • Loading branch information...
tyler6699 committed Jul 21, 2019
1 parent 0e4eca3 commit 63bf2d77cff4259ac3afb2864bce7e466aebb325
@@ -28,6 +28,7 @@
public float angle;
public int direction;
public Compass facing;
public boolean spacePressed;

// MOUSE
public boolean leftMouseBtn;
@@ -91,6 +92,9 @@ public boolean keyDown(int keyCode) {
case Keys.D:
right = true;
break;
case Keys.SPACE:
spacePressed = true;
break;
}
return false;
}
@@ -20,7 +20,8 @@
public enum EntityType {
HERO,
TREE,
BIRD
BIRD,
BULLET
}

/**
@@ -2,6 +2,7 @@

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

import uk.co.carelesslabs.box2d.Box2DWorld;
import uk.co.carelesslabs.entity.Bird;
import uk.co.carelesslabs.entity.Entity;
@@ -12,6 +13,8 @@
import uk.co.carelesslabs.map.Tile;
import uk.co.carelesslabs.map.Island;
import uk.co.carelesslabs.ui.SquareMenu;
import uk.co.carelesslabs.weapons.Gun;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
@@ -112,7 +115,7 @@ public void render() {
control.processedClick = squareMenu.build.checkClick(control.mouseClickPos, control.processedClick);
squareMenu.checkHover(control.mousePos);

hero.update(control);
hero.update(control, box2D);

// Hero Position
if (Rumble.getRumbleTimeLeft() > 0){
@@ -174,6 +177,9 @@ public void render() {
box2D.tick(camera, control);
island.clearRemovedEntities(box2D);

// Clear travelled bullets
hero.clearAmmo(box2D);

time += Gdx.graphics.getDeltaTime();
if(time > 3){
if(control.debug) System.out.println(Gdx.graphics.getFramesPerSecond());
@@ -1,6 +1,8 @@
package uk.co.carelesslabs.entity;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
@@ -10,6 +12,7 @@
import uk.co.carelesslabs.Enums.EntityType;
import uk.co.carelesslabs.box2d.Box2DHelper;
import uk.co.carelesslabs.box2d.Box2DWorld;
import uk.co.carelesslabs.entity.ammo.SimpleBullet;
import uk.co.carelesslabs.weapons.Gun;

public class Hero extends Entity{
@@ -28,8 +31,13 @@ public Hero(Vector3 pos, Box2DWorld box2d){
reset(box2d, pos);

// Weapon
Gun gun = new Gun(1, -1, 7);
weapons = new ArrayList<Gun>();
weapons.add(new Gun(1, -1, 7));
weapons.add(gun);

// Add some ammo to the gun
weapons.get(0).addAmmo(10);

}

public Hero(JsonObject e, Box2DWorld box2d) {
@@ -53,6 +61,7 @@ public void draw(SpriteBatch batch){
if(texture != null) batch.draw(texture, pos.x, pos.y, width, height);
for(Gun g : weapons){
if(g.active){
g.tick(Gdx.graphics.getDeltaTime());
g.drawRotated(batch);
}
}
@@ -66,7 +75,7 @@ public void reset(Box2DWorld box2d, Vector3 pos) {
inventory.reset();
}

public void update(Control control) {
public void update(Control control, Box2DWorld box2D) {
dirX = 0;
dirY = 0;

@@ -78,7 +87,7 @@ public void update(Control control) {
body.setLinearVelocity(dirX * speed, dirY * speed);
pos.x = body.getPosition().x - width/2;
pos.y = body.getPosition().y - height/4;

// If interact key pressed and interactEntities present interact with first in list.
if(control.interact && interactEntities.size() > 0){
interactEntities.get(0).interact(this);
@@ -88,7 +97,14 @@ public void update(Control control) {
for(Gun g : weapons){
if(g.active){
g.updatePos(pos.x, pos.y);
g.angle = control.angle - 90;
g.angle = control.angle - 90;

if(control.spacePressed){

SimpleBullet bullet = new SimpleBullet(g, box2D);
g.addActiveAmmo(bullet);
control.spacePressed = false;
}
}
}

@@ -119,5 +135,11 @@ public boolean weaponActive() {
}
return false;
}

public void clearAmmo(Box2DWorld box2D) {
for(Gun g : weapons){
g.clearTravelledAmmo(box2D);
}
}

}
@@ -0,0 +1,21 @@
package uk.co.carelesslabs.entity.ammo;

import com.badlogic.gdx.math.Vector2;
import uk.co.carelesslabs.entity.Entity;

public class Ammo extends Entity {
public float range;
public float damage;
public Vector2 vector;
public float distMoved;

public Ammo(){
super();
vector = new Vector2();
}

public void tick(float delta){

}

}
@@ -0,0 +1,63 @@
package uk.co.carelesslabs.entity.ammo;

import uk.co.carelesslabs.Media;
import uk.co.carelesslabs.Enums.EntityType;
import uk.co.carelesslabs.box2d.Box2DHelper;
import uk.co.carelesslabs.box2d.Box2DWorld;
import uk.co.carelesslabs.weapons.Gun;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef;

public class SimpleBullet extends Ammo {
Gun gun;

public SimpleBullet(Gun gun, Box2DWorld box2d){
super();
this.gun = gun;
type = EntityType.BULLET;
texture = Media.close_menu;
range = 50;
damage = 1;
width = 8;
height = 8;
speed = 60;
active = true;
setupBullet(box2d);
}

public void tick(float delta){
if(active){
float dx = (delta * vector.x) * speed;
float dy = (delta * vector.y) * speed;
float dx2 = pos.x + dx;
float dy2 = pos.y + dy;

distMoved += Vector2.dst(pos.x, pos.y, dx2, dy2);
pos.set(dx2, dy2, 0);
sensor.setTransform(pos.x + width/2, pos.y + height/2, 0);

if(distMoved > range){
// Remove Bullet
remove = true;
active = false;
}
}

}

public void setupBullet(Box2DWorld box2d){
// Position
float angleRadians = MathUtils.degreesToRadians * gun.angle;
vector.x = MathUtils.cos(angleRadians);
vector.y = MathUtils.sin(angleRadians);

// Move bullet toward end of gun
pos.x = gun.pos.x + (vector.x * 10);
pos.y = gun.pos.y + (vector.y * 10);

// Physics
sensor = Box2DHelper.createSensor(box2d.world, width, height*.85f, width/2, height/3, pos, BodyDef.BodyType.DynamicBody);
hashcode = sensor.getFixtureList().get(0).hashCode();
}
}
@@ -1,15 +1,21 @@
package uk.co.carelesslabs.weapons;

import java.util.ArrayList;
import java.util.Iterator;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import uk.co.carelesslabs.Media;
import uk.co.carelesslabs.box2d.Box2DWorld;
import uk.co.carelesslabs.entity.Entity;
import uk.co.carelesslabs.entity.ammo.Ammo;

public class Gun extends Entity {
float originXOffset; // OriginX Offset
float originYOffset; // OriginY Offset
float xPos; // X offset for gun position
float xMinPos; // X Position offset facing left
float xMaxPos; // X Position offset facing right
float ammoCount;
public ArrayList<Ammo> activeAmmo;

public Gun(float originXOffset, float xMinRight, float xMaxRight){
texture = Media.gun;
@@ -20,6 +26,22 @@ public Gun(float originXOffset, float xMinRight, float xMaxRight){
this.originXOffset = originXOffset;
this.xMinPos = xMinRight;
this.xMaxPos = xMaxRight;
activeAmmo = new ArrayList<Ammo>();
}

public void tick(float delta){
for(Ammo a : activeAmmo){
a.tick(delta);
}
}

public void addActiveAmmo(Ammo a){
if(ammoCount > 0){
activeAmmo.add(a);
ammoCount --;
} else {
System.out.println("Clink");
}
}

public void drawRotated(SpriteBatch batch){
@@ -32,6 +54,28 @@ public void drawRotated(SpriteBatch batch){
}

if(texture != null) batch.draw(texture, pos.x + xPos, pos.y, originXOffset, originYOffset, width, height, 1, 1, angle, 0, 0, (int)width, (int)height, flipX, flipY);
}

for(Ammo a : activeAmmo){

a.draw(batch);
}
}

public void addAmmo(int count) {
ammoCount += count;
}

public void clearTravelledAmmo(Box2DWorld box2D) {
Iterator<Ammo> it = activeAmmo.iterator();
while(it.hasNext()) {
Ammo a = it.next();
if(a.remove){
a.removeBodies(box2D);
box2D.removeEntityToMap(a);

it.remove();
}
}
}

}

Large diffs are not rendered by default.

0 comments on commit 63bf2d7

Please sign in to comment.
You can’t perform that action at this time.