I'm having a strange issue which I can't understand. I'm trying to apply an isNPC
boolean to a PlayerMP
object through the constructor, so I can cause NPC's (non-player characters) to move without user input. I've created a tick()
method to override the super class, while calling it again when the player is not an NPC (allowing the player to use keyboard inputs to move the character), as can be seen below:
public class PlayerMP extends Player {
public InetAddress ipAddress;
public int port;
protected boolean isNPC; //used in server class to prevent sending of packets to NPC entities registered in PlayerMP list.
public PlayerMP(Level lvl, int x, int y, InputHandler ipt, String usr, InetAddress ipAdd, int prt, int col) {
super(lvl, x, y, ipt, usr, col);
ipAddress = ipAdd;
port = prt;
isNPC = false;
}
public PlayerMP(Level lvl, int x, int y, String usr, InetAddress ipAdd, int prt, int col) {
super(lvl, x, y, usr, col);
ipAddress = ipAdd;
port = prt;
isNPC = false;
}
public PlayerMP(Level lvl, int x, int y, String usr, InetAddress ipAdd, int prt) { //constructor for local player
super(lvl, x, y, null, usr);
ipAddress = ipAdd;
port = prt;
isNPC = false;
}
public PlayerMP(Level lvl, int x, int y, String usr, int col) {
super(lvl, x, y, usr, col);
isNPC = true;
}
@Override
public synchronized void tick(){
if(!isNPC) {
super.tick();
}
else{
int xa = 0; //reset xa and ya so character stops when no keys are pressed (i.e. doesn't move continuously)
int ya = 0;
xa++;
if (xa != 0 || ya != 0) {
move(xa, ya);
isMoving = true;
Packet02Move packet = new Packet02Move(getUsername(), x, y, 050, noSteps, isMoving, movingDir); //sending move pack to let server/other clients know that this character is moving + where to
packet.writeData(GameApplication.game.client);
} else {
isMoving = false;
}
if (level.getTile(x >> 3, y >> 3).getId() == 3) { //when tile is water
isSwimming = true;
}
if (isSwimming && level.getTile(x >> 3, y + 3 >> 3).getId() != 3) { //return to normal when tile is no longer water (y+3 to allow for collision rather than rendering coords)
isSwimming = false;
}
if (level.getTile(x >> 3, y >> 3).getId() == 8 || level.getTile(x >> 3, y >> 3).getId() == 9 || level.getTile(x >> 3, y >> 3).getId() == 10) { //when tile is tree trunk or leaves
isBehind = true;
}
//x + y coords need tweaking
if (isBehind && level.getTile(x+3 >> 3, y >> 3).getId() != 8 && level.getTile(x+3 >> 3, y >> 3).getId() != 9 && level.getTile(x+3 >> 3, y >> 3).getId() != 10) { //return to normal when tile is no longer tree trunk/leaves
isBehind = false;
}
}
}
public boolean isNPC(){return isNPC;}
}
The xa++;
causes to character's x co-ordinate to increase, i.e. it moves to the right - this is simply to test movement, but when I add these NPC entities using the above constructor which initialises isNPC
to true, they still execute super.tick()
instead of the else{}
block. Is there any reason why this would be happening?
When I change !isNPC
to isNPC
in the tick()
method, it moves to the right, as expected, so I know the code is working.
EDIT: Super class constructor below + full PlayerMP
class code above:
public Player(Level level, int x, int y, String usrname, int col) {
super(level, "Player", x, y, 1);
shortColour = col;
setBodyColour(col);
username = usrname;
}
Initialisation of PlayerMP
object with correct constructor (only one has 5 parameters):
PlayerMP npc2 = new PlayerMP(game.level, 316, 250, "NPC Gerald", 020);
I should also add that the isNPC variable is working as intended for other purposes such as the directing of packets to clients in the server class.
Constructor for Player's superclass (Mob):
public Mob(Level lvl, String nm, int x, int y, int spd) {
super(lvl);
name = nm;
this.x = x;
this.y = y;
speed = spd;
}
EDIT: Anyone else have any suggestions? isNPC is applying in some constructors as expected and won't work in others....