Hello, all. Today I am facing a really confusing problem that I am not sure of.
Its all with firing projectiles.
The expected behavior (for testing) is to simple move left.
The behavior I am getting instead is not only moving to the left, but drawing at the wrong size, in fact the same size as the player, plus dragging the player along with it.
May one of you kind people analyze my code a little, and maybe point out what might be the cause of this?
Here is the Entity class:
The Player class (problem must lie here):package cjaf.entity; import java.awt.Rectangle; import org.newdawn.slick.opengl.Texture; import cjaf.entity.event.EntityDamageEvent; import cjaf.item.Item; import cjaf.main.Location; import cjaf.tools.AdvancedDrawer; public abstract class Entity { public Item[] drops; protected boolean hasDrops; protected Location loc; protected Texture texture; protected AdvancedDrawer renderer = new AdvancedDrawer(); public byte health, maxHealth; public int width, height; protected Rectangle bounds; public int speed; public Location getLocation(){ return loc; } public void setLocation(Location loc){ this.loc = loc; } public byte getHealth(){ return health; } public byte getMaxHealth(){ return maxHealth; } public void setTexture(String file){ texture = renderer.getTexture(file); } public void draw(){ renderer.drawTexture(texture, loc.getX(), loc.getY(), 64, 64); } public int getWidth(){ return width; } public int getHeight(){ return height; } //Should be replaced. public void tick(){ } public void damage(byte amount){ EntityDamageEvent ede = new EntityDamageEvent(this, amount, 1000); ede.call(); } public boolean intersects(Entity ent){ if(bounds.intersects(ent.bounds)&&!ent.isDead()){ return true; } return false; } public void setBounds(Location loc, int width, int height){ this.width = width; this.height = height; bounds = new Rectangle(); bounds.setBounds(loc.getX(), loc.getY(), width, height); } public boolean isDead(){ if(this.getHealth() <= 0) return true; else return false; } public void setHealth(byte b){ this.health = b; } public boolean isClose(Entity ent, int distance){ distance*=64; if(ent.getLocation().getX() > this.getLocation().getX()-distance && ent.getLocation().getX() < this.getLocation().getX()+distance){ if(ent.getLocation().getY() > this.getLocation().getY()-distance && ent.getLocation().getY() < this.getLocation().getY()+distance){ return true; } } return false; } public boolean isToLeftOf(Entity ent){ if(this.getLocation().getX() < ent.getLocation().getX()) return true; else return false; } public boolean isToRightOf(Entity ent){ if(this.getLocation().getX() > ent.getLocation().getX()) return true; else return false; } public boolean isAbove(Entity ent){ if(this.getLocation().getY() < ent.getLocation().getY()) return true; else return false; } public boolean isBelow(Entity ent){ if(this.getLocation().getY() > ent.getLocation().getY()) return true; else return false; } public void dropItem(Item item){ item.popAt(getLocation()); } public boolean hasDrops(){ return hasDrops; } public void setCanHaveDrops(boolean val){ hasDrops = val; } public void updateLocation(){ bounds.setLocation(loc.getX(), loc.getY()); } public void setSpeed(int speed){ this.speed = speed; } public Item[] getDrops(){ return drops; } }
package cjaf.entity; import java.util.ArrayList; import java.util.Random; import org.lwjgl.input.Keyboard; import cjaf.entity.entityais.EntityAI; import cjaf.entity.entityais.EntityAICollide; import cjaf.entity.entityais.EntityAIInteractEntities; import cjaf.entity.entityais.EntityAIShoot; import cjaf.item.Item; import cjaf.main.Location; /** * Player class. * @author JavaCakess@CJAF * */ public class Player extends Entity implements EntityAI, EntityAIInteractEntities, EntityAICollide, EntityAIShoot{ int fire = Keyboard.KEY_SPACE; private int shootTicks; private ArrayList<Bullet> bullets; public Player(Location loc){ this.loc = loc; setBounds(getLocation(), 52, 52); setTexture("entities/player/player.png"); this.maxHealth = 10; this.health = 10; bullets = new ArrayList<Bullet>(); } public void tick() { //Update entity if(Keyboard.isKeyDown(Keyboard.KEY_W)){ getLocation().add(0, -2); } if(Keyboard.isKeyDown(Keyboard.KEY_A)){ getLocation().add(-2, 0); } if(Keyboard.isKeyDown(Keyboard.KEY_S)){ getLocation().add(0, 2); } if(Keyboard.isKeyDown(Keyboard.KEY_D)){ getLocation().add(2, 0); } bounds.setLocation(loc.getX(), loc.getY()); } public void respawn(){ this.health = maxHealth; Random r = new Random(); int x = r.nextInt(640); int y = r.nextInt(480); this.setLocation(new Location(x, y)); } public void tick(ArrayList<Entity> e) { for(Entity ent : e){ if(Keyboard.isKeyDown(fire)){ if(this.intersects(ent)&&!(ent == this)&&!ent.isDead()){ //ent.damage((byte) 1); } } } } public void shoot(ArrayList<Entity> e) { /* * */ shootTicks++; if(Keyboard.isKeyDown(fire)&&shootTicks > 40){ //TODO: Add firing logic. bullets.add(new Bullet(this.getLocation())); } if(shootTicks == 60){ shootTicks = 0; } for(Bullet b : bullets){ b.draw(); b.tick(); } } }
And the Bullet class itself:
package cjaf.entity; import cjaf.main.Location; public class Bullet extends Entity{ public Bullet(Location loc){ this.loc = loc; setBounds(getLocation(), 8, 8); setTexture("entities/projectiles/bullet.png"); this.maxHealth = 1; this.health = 1; } public void tick(){ getLocation().add(0, 1); } }
The getLocation() returns a location object. The location class only has 2 variables, x and y.
I should also probably show you the EntityHandler class, which handles all the onscreen entities.
import java.util.ArrayList; import java.util.Random; import cjaf.entity.BasicEntity; import cjaf.entity.Entity; import cjaf.entity.Ghost; import cjaf.entity.Player; import cjaf.entity.Sword; import cjaf.entity.entityais.EntityAIAttackPlayer; import cjaf.entity.entityais.EntityAICollide; import cjaf.entity.entityais.EntityAIInteractEntities; import cjaf.entity.entityais.EntityAIShoot; import cjaf.tools.AdvancedDrawer; /** * Handles onscreen Entities * @author JavaCakess@CJAF * */ public class EntityHandler { AdvancedDrawer renderer = new AdvancedDrawer(); volatile ArrayList<Entity> e; Player p; BasicEntity b; BasicEntity bee; Ghost g; Sword xs; Random r = new Random(); int handleTicks = 0; public EntityHandler(ArrayList<Entity> e){ this.e = e; } public void init(){ int x = r.nextInt(640); int y = r.nextInt(480); p = new Player(new Location(x, y)); b = new BasicEntity(new Location(1, 1)); g = new Ghost(renderer.getCenterOfScreen()); xs = new Sword(new Location(640, 475)); e.add(p); //e.add(b); e.add(xs); //e.add(g); } public void handle(){ handleTicks++; if(handleTicks == seconds(7)){ e.add(new BasicEntity(new Location(r.nextInt(640), r.nextInt(480)))); handleTicks = 0; } for(Entity entity : e){ if(!entity.isDead()){ entity.draw(); //Render entities. entity.tick(); //Update entities by calling each of their tick() methods. if(entity instanceof EntityAIAttackPlayer){ EntityAIAttackPlayer e = (EntityAIAttackPlayer) entity; e.tick(p); } if(entity instanceof EntityAICollide){ EntityAICollide e = (EntityAICollide) entity; e.tick(this.e); } if(entity instanceof EntityAIInteractEntities){ EntityAIInteractEntities e = (EntityAIInteractEntities) entity; e.tick(this.e); } if(p.isDead()){ System.err.println("You died!!!"); p.respawn(); } } } } public int seconds(int seconds){ return 60*seconds; } public Player getCurrentPlayer(){ return p; } }
Thanks for all the help