Search code examples
javaoopslick2d

Java, Slick, An object that shouldnt move is moving?


In my game I'm making, I need NPC's/mobs. I made a class called peasant (my first mob). In the main class from which the game is running, I have all the information, as well as calling an object called mob1 from Peasant I need to make it so that if the player is within 300 pixels of the mob, it starts to move towards it. Ive tried doing this but so far, even if the player is 2000 pixels away, the mob starts moving???

Here is my Peasant class

package Entitys.NPCs;


public class Peasant {

    public Peasant(float InitX, float InitY, int MobID){
        MobX = InitX;
        MobY = InitY;
    }
    //This class shall be used as an object creator. This will randomly move a graphic around, near to a player
    private float MobX;
    private float MobY;
    private int AmountMoved = 0;
    private boolean MoveRight = true;
    private boolean MoveLeft;
    public boolean PlayerDetected = false;
    long timer;
    //Used to find the mobs X
    public float getX(){
        return MobX;
    }

    //Used to find the mobs Y
    public float getY(){
        return MobY;
    }

    //Used to set the mobs X
    public void setX(float X){
        MobX = X;
    }   

    //Used to set the mobs Y
    public void setY(float Y){
        MobY = Y;
    }

    //Used to simply move the mob on its X co-ords
    public void moveX(int delta){
        MobX += delta*0.1f;
    }

    //Used to simply move the mob on its Y co-ords
    public void moveY(int delta){
        MobY += delta*0.1f;
    }

    public void autoEveryThing(int delta, float playerX, float playerY) {
        // If the player has not been spotted the NPC/Mob will move left and
        // right by 100 Pixels.
        if(MobX-300<playerX){
            PlayerDetected=true;
        }

        if(PlayerDetected=true){
            if(MobX>playerX){
                MobX-=delta*0.12;
            }
        }

    }

}

And here is the main class:

    package Worlds.World1;

import org.lwjgl.input.Mouse;
import org.newdawn.slick.Animation;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import Entitys.NPCs.*;

import Main.SimpleMob;

public class World1A extends BasicGameState{
    String mousePosition;
    Image world;
    Animation player, playerLeft, playerRight;
    int[] duration = {200,200};
    float playerX;
    float playerY;
    float WorldX;
    float WorldY;
    float PlayerVisibleScreenX;
    float PlayerVisibleScreenY;
    String MovementDirection;
    Peasant mob1;
    public World1A(int state){
    }

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
        Image [] WalkingLeft = {new Image("res/Sprites/buckysLeft.png"),new Image("res/Sprites/buckysLeft.png")};
        Image [] WalkingRight = {new Image("res/Sprites/buckysRight.png"),new Image("res/Sprites/buckysRight.png")};

        playerLeft = new Animation(WalkingLeft, duration, false);
        playerRight = new Animation(WalkingRight, duration, false);
        player = playerRight;
        WorldX = 0;
        WorldY = 0;
        world= new Image("res/WorldImages/WorldBackGround.png");
        mousePosition="null";
        MovementDirection = "Not Moved Yet";
        mob1= new Peasant(2000, 200, 1);
        if(WorldX<=0){
            playerX = Math.abs(WorldX);
        }else{
            playerX=0-WorldX;
        }
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
        player.draw(450, 300);
        if(WorldX>0){
            world.draw(0, 0);
            g.fillOval(0+mob1.getX(), 0+mob1.getY(), 50, 50);
            g.fillRect(0, 0+340, 500, 10);
            player.draw(-WorldX + 450, 300);
        }else{
            world.draw(WorldX, WorldY);
            g.fillOval(WorldX+mob1.getX(), WorldY+mob1.getY(), 50, 50);
            g.fillRect(WorldX, WorldY+340, 500, 10);
            g.fillOval(WorldX+mob1.getX(), WorldY+mob1.getY(), 50, 50);
            player.draw(450, 300);
        }
        g.setColor(Color.white);

        //All this is co-ords ect, it is for developement help
        g.drawString(String.valueOf(mob1.getX()), 50, 200);
        g.drawString("World X: "+ String.valueOf(WorldX), 50, 225);
        g.drawString("Player X: "+ String.valueOf(playerX), 50, 250);
        g.drawString("Player Detetcted?: "+ String.valueOf(mob1.PlayerDetected), 50, 265);
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
        if(WorldX<=0){
            playerX = Math.abs(WorldX);
        }else{
            playerX=0-WorldX;
        }
        mob1.autoEveryThing(delta, playerX, playerY);
        int posX = Mouse.getX();
        int posY = Mouse.getY();
        mousePosition = "X: " + posX + "\nY: " + posY;

        Input input = gc.getInput();
        if(input.isKeyDown(Input.KEY_LEFT)){
            WorldX += delta * 0.1f;
            MovementDirection = "Left";
            player = playerLeft;
        }else if(input.isKeyDown(Input.KEY_RIGHT)){
            WorldX -= delta * 0.1f;
            MovementDirection = "Right";
            player = playerRight;
        }else{
            MovementDirection = "Not Moving";
        }
    }


    //DO NOT CHANGE
    public int getID(){
        return 2;
    }

}

The autoEveryThing method in the Peasant class should make it so that if(mobX-300

But when I first run this it starts moving towards the player? even though (2000-300<0) is false, it still sets the PlayerDetected boolean to true??? Why does this happen? Thanks

EDIT: After trying to go through thi and fix it I found somthing strange, even if I take out the whole bit which can change PlayerDetected to true, the mob still moves towards the player. This meens that PlayerDetected is becominbg true somwhere, but I cant figure out where?


Solution

  •    if(PlayerDetected=true){
    

    is wrong you should use ==

       if(PlayerDetected==true){
    

    or even better

    boolean isPlayerDetected;
    
    if (isPlayerDetected) 
    

    further consider

     double dx = mobX - playerX;
     double dy = mobY - playerY;
     double distance = Math.sqrt(dx*dx + dy*dy);
     if (distance < 300) {
         // is within distacne threshold
     }