Félix Montreuil
Félix Montreuil

Reputation: 35

Java: Firing projectile in different direction without affecting projectile already sent

So basically, when I change direction the projectile I shoot follows along just fine. The problem is that all the previous projectile also changes direction.

this is my projectile.java :

package dev.codenmore.tilegame.gfx;

import java.awt.Graphics;

import dev.codenmore.tilegame.Handler;
import dev.codenmore.tilegame.entity.creatures.Player;

public class Projectile {

    private double x;
    private double y;

    public Projectile (double x, double y, Handler handler){
        this.x = x;
        this.y = y;
    }

    public void tick(){
        if ( Player.getPos() == 3)
            x += 10;
        if ( Player.getPos() == 2)
            x -= 10;
        if (Player.getPos() == 1)
            y -= 10;
        if (Player.getPos() == 0)
            y += 10;
    }

    public void render(Graphics g){
        g.drawImage(Assets.arrow, (int) x, (int) y, null);
    }

}

this is my controller.java:

package dev.codenmore.tilegame;

import java.awt.Graphics;
import java.util.LinkedList;

import dev.codenmore.tilegame.gfx.Projectile;

public class Controller {

    private static LinkedList<Projectile> b = new LinkedList <Projectile>();

    Projectile TempProjectile;

    Handler handler;

    public Controller(Handler handler){
        this.handler = handler;


    }

    public void tick(){
        for(int i = 0; i < b.size(); i++){
            TempProjectile = b.get(i);

            TempProjectile.tick();

        }
    }

    public void render(Graphics g){
        for(int i = 0; i < b.size(); i++){
            TempProjectile = b.get(i);

            TempProjectile.render(g);

        }

    }

    public static void addProjectile(Projectile block){
        b.add(block);
    }
    public static void removeProjectile(Projectile block){
        b.remove(block);
    }    
}

My player.java:

package dev.codenmore.tilegame.entity.creatures;

import java.awt.Color;
import java.awt.Graphics;

import dev.codenmore.tilegame.Controller;
import dev.codenmore.tilegame.Game;
import dev.codenmore.tilegame.Handler;
import dev.codenmore.tilegame.gfx.Assets;
import dev.codenmore.tilegame.gfx.Projectile;

public class Player extends Creature {

    public static int pos = 0;



    public Player(Handler handler, float x, float y) {
        super(handler, x, y, Creature.DEFAULT_CREATURE_WIDTH, Creature.DEFAULT_CREATURE_HEIGHT);

        bounds.x = 46;
        bounds.y = 64;
        bounds.width = 32;
        bounds.height = 44;

    }

    @Override
    public void tick() {
        getInput();
        move();
        handler.getGameCamera().centerOnEntity(this);
    }

    private void getInput(){
        xMove = 0;
        yMove = 0;

        if(handler.getKeyManager().up)
            yMove = -speed;
        if(handler.getKeyManager().down)
            yMove = speed;
        if(handler.getKeyManager().left)
            xMove = -speed;
        if(handler.getKeyManager().right)
            xMove = speed;
        if(handler.getKeyManager().shoot)
            Controller.addProjectile(new Projectile((double)(x - handler.getGameCamera().getxOffset()),  (double)(y - handler.getGameCamera().getyOffset()), handler));

    }

    public static int getPos(){
        return pos;
    }

    @Override
    public void render(Graphics g) {



        if ( pos == 0)
            g.drawImage(Assets.player, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
        if (pos == 1)
            g.drawImage(Assets.playerUp, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
        if (pos == 2)
            g.drawImage(Assets.playerLeft, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
        if (pos == 3)
            g.drawImage(Assets.playerRight, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
        if(handler.getKeyManager().up){
            g.drawImage(Assets.playerUp, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
            pos = 1;
        }else if(handler.getKeyManager().left){
            g.drawImage(Assets.playerLeft, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
            pos = 2;
        }else if(handler.getKeyManager().right){
            g.drawImage(Assets.playerRight, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
            pos = 3;
        }else if(handler.getKeyManager().down){
            g.drawImage(Assets.player, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
            pos = 0;
        }

    }

}

I'm fairly new to programming so sorry for the mistakes. This is what happens: http://gyazo.com/118b525d22e42a92fece77f70201cdad

Upvotes: 0

Views: 94

Answers (1)

maskacovnik
maskacovnik

Reputation: 3084

Problem is there:

public void tick(){
    if ( Player.getPos() == 3)
        x += 10;
    if ( Player.getPos() == 2)
        x -= 10;
    if (Player.getPos() == 1)
        y -= 10;
    if (Player.getPos() == 0)
        y += 10;
}

In each tick player is asked on his direction, which can be changed, so the projectiles will change too. To avoid it, save the position for each projectile at start like this:

private int pos;
public Projectile (double x, double y, Handler handler){
    this.x = x;
    this.y = y;
    this.pos = Player.getPos();
}

and then tick method:

public void tick(){
    if ( pos == 3)
        x += 10;
    if ( pos == 2)
        x -= 10;
    if ( pos == 1)
        y -= 10;
    if ( pos == 0)
        y += 10;
}

Switch will be better:

public void tick(){
    switch(pos){
        case 3: x+=10; break;
        case 2: x-=10; break;
        case 1: y-=10; break;
        case 0: y+=10; break;
    }
}

Upvotes: 3

Related Questions