I'm working on an enemy called a "Charger". The idea is that once the player gets within 250 pixels of it, it will charge in a straight direction for 240 pixels, not altering its path until it is finished charging. HOWEVER. I have noted the following behaviour:
Either
a. The charger does absolutely nothing.
or
b. The charger moves in a vertical line for a second, then stops. It never moves again.
The idea is a little bit ahead of my coding abilities, so I'm not entirely sure of what I'm doing while I write this. Here is the class (it's a fair bit of code, bear with me):
What am I doing wrong? Or is the idea just beyond me at the moment? I'm fine with it if I have to give up on it, there's lots more types of enemies I'm going to code.
public class Charger extends Enemy { Room room; Stats stats; Character character; String collisionSide; double realXloc, realYloc; double health = 10; int chargeRemaining = 0; int recharge = 0; double moveSpeed = 4; double yMomentum, xMomentum, xDistance, yDistance = 0; private String chargePlayer = "false"; public void act() { damageTimer(); checkCollision(); chargeAtPlayer(); moveMe(); //findPlayer(); } public void damageTimer() { if(recharge > 0) { recharge = (recharge - 1); } } public void addedToWorld(World world) { room = (Room)getWorld(); stats = room.stats; character = room.character; realXloc = getX(); realYloc = getY(); } public void checkCollision() { collisionSide = detectCollisionReturnSide((int)moveSpeed, (int)xMomentum, (int)yMomentum); if("Right".equals(collisionSide) || "Left".equals(collisionSide)) { //determine up or down if(yDistance > 0) { yMomentum = moveSpeed; } else { yMomentum = -moveSpeed; } xMomentum = 0; } if("Up".equals(collisionSide) || "Down".equals(collisionSide)) { //determine right or left if(xDistance > 0) { xMomentum = moveSpeed; } else { xMomentum = -moveSpeed; } yMomentum = 0; } } public void findPlayer() { xDistance = (character.getX() - getX()); yDistance = (character.getY() - getY()); if(Math.abs(xDistance) > Math.abs(yDistance) && Math.abs(xDistance) < 250) { chargeRemaining = 60; chargePlayer = "x"; } else if(Math.abs(yDistance) > Math.abs(xDistance) && Math.abs(xDistance) < 250) { chargeRemaining = 60; chargePlayer = "y"; } else { chargePlayer = "false"; } } public void chargeAtPlayer() { if("x".equals(chargePlayer)) { if(xDistance > 0) { xMomentum = moveSpeed; } else { xMomentum = -moveSpeed; } yMomentum = 0; chargeRemaining = (chargeRemaining - 1); if(chargeRemaining <= 0) { chargePlayer = "false"; } } if("x".equals(chargePlayer)) { if(yDistance > 0) { yMomentum = moveSpeed; } else { yMomentum = -moveSpeed; } xMomentum = 0; chargeRemaining = (chargeRemaining - 1); if(chargeRemaining <= 0) { chargePlayer = "false"; } } if("false".equals(chargePlayer)) { yMomentum = 0; xMomentum = 0; findPlayer(); } } public void moveMe() { realXloc = realXloc + xMomentum; realYloc = realYloc + yMomentum; setLocation((int)realXloc, (int)realYloc); checkBoundaries(); if(getOneIntersectingObject(Character.class) != null && recharge <= 0) { recharge = 60; stats.damagePlayer(1); } } public void damageEnemy(double amount) { BloodSplat bloodsplat = new BloodSplat(); getWorld().addObject(bloodsplat, getX(), getY()); bloodsplat.setTrans((int)((10 - health) * 25.5)); health = (health - amount); if(health <= 0) { getWorld().removeObject(this); } } }