Java / LWJGL:无理由执行两次代码

时间:2016-01-26 22:48:37

标签: java opengl lwjgl

我正在开发LWJGL游戏。我有一个项目代码可以将项目添加到玩家的广告资源中,但每当我调用它时,它都会将该项目添加到广告位(x)和广告位之后的广告位(x )。 (x表示任何随机数)我希望我可以帮助调试它。

正在调用类add方法:

package geniushour.gameobject.entity;

import static org.lwjgl.input.Keyboard.KEY_A;
import static org.lwjgl.input.Keyboard.KEY_D;
import static org.lwjgl.input.Keyboard.KEY_P;
import static org.lwjgl.input.Keyboard.KEY_SPACE;
import static org.lwjgl.input.Keyboard.isKeyDown;

import java.util.ArrayList;
import java.util.Random;

import geniushour.engine.Physics;
import geniushour.engine.time.Delay;
import geniushour.engine.time.Time;
import geniushour.game.Game;
import geniushour.game.Util;
import geniushour.game.type.ArmorType;
import geniushour.game.type.ClawType;
import geniushour.game.type.EntityType;
import geniushour.gameobject.GameObject;
import geniushour.gameobject.StatObject;
import geniushour.gameobject.item.Backpack;
import geniushour.gameobject.item.Item;

public class Player extends StatObject {

private Delay delay;
private boolean collide = false;
private int facingDirection = 0;
private int firstFree = 0;

private Random damageRandom;

private final int NORTH = 0;
private final int EAST = 1;
private final int SOUTH = 2;
private final int WEST = 3;

public Player(float x, float y){
    super(true,0,6);
    init(x,y,ENTITY_SIZE,ENTITY_SIZE,0.25f,1f,.25f,EntityType.PLAYER_ID.getID());
    deleteBackpack();
    setClaw(ClawType.BASIC);
    setArmorType(ArmorType.SKIN);
    setStrength(1, claw);
    delay = new Delay(500);     delay.terminate();
}
public void update(){
    ArrayList<GameObject> items = Game.rectCollide(x, y, x+ENTITY_SIZE, y+ENTITY_SIZE);
    for(GameObject go : items){
        if(go.getType() == EntityType.ITEM_ID.getID()){
            go.remove();
            inv.add ((Item)go);
        }
    }
    ArrayList<GameObject> wall = Game.rectCollide(x, y, x+ENTITY_SIZE, y+ENTITY_SIZE);
    for(GameObject go : wall){
        if(go.getType() == EntityType.BLOCK_ID.getID()){
            if(Physics.checkCollision(this, go) != null){
                collide = true;
            }
            else if(Physics.checkCollision(this, go) == null){
                collide = false;
            }
        }
        if(collide){
            if(this.facingDirection == WEST){
                move(1,0);
                collide = false;
            }
            else if(this.facingDirection == EAST){
                move(-1,0);
                collide = false;
            }
        }
    }

    if(getCurrentHealth() <= 0){
        addHP(-getCurrentHealth());
        deleteBackpack(); //TODO: Backpack temp or not?
    }

    for(int i = 0; i < inv.getLength(); i++){
        if(inv.get( i ) instanceof Backpack){
            ownBackpack();
            inv.remove(i);
            inv.setSlots(27);
        }
    }
}

    outputData(true);
}
public void getInput(){
    if(!collide){
        if(isKeyDown(KEY_A)){
            move(-1,0);
        }
        if(isKeyDown(KEY_D)){
            move(1,0);
        }
        if(isKeyDown(KEY_SPACE) && delay.over()){
            attack();
        }
        if(isKeyDown(KEY_P)){
        }
    }
}
private void move(int magX, int magY){
    if(magX == 1){
        facingDirection = EAST;
    }
    if(magX == -1){
        facingDirection = WEST;
    }

    x += getSpeed(armor) * magX * Time.getDelta();
    y += getSpeed(armor) * magY * Time.getDelta();
}
private void attack(){
    //Find GameObjects in Attack Range
    ArrayList<GameObject> obj = new ArrayList<GameObject>();

    switch(facingDirection){
        case NORTH: obj = Game.rectCollide(x, y, x + ENTITY_SIZE, y + getCloseAttackRange());
            break;
        case EAST: obj = Game.rectCollide(x, y, x + getCloseAttackRange(), y + ENTITY_SIZE);
            break;
        case SOUTH: obj = Game.rectCollide(x, y - getCloseAttackRange() + ENTITY_SIZE, x + ENTITY_SIZE, y);
            break;
        case WEST: obj = Game.rectCollide(x - getCloseAttackRange() + ENTITY_SIZE, y, x, y + ENTITY_SIZE);
            break;
    }

    //Find which GOs are Enemy Type
    ArrayList<Enemy> enemies = new ArrayList<Enemy>();
    for(GameObject go : obj){
        if(go.getType() == EntityType.ENEMY_ID.getID()){
            enemies.add((Enemy)go);
        }
    }

    //Find closest existing enemy
    if(enemies.size() > 0){

        Enemy target = enemies.get(0);
        if(enemies.size() > 1){
            for(Enemy e : enemies){
                if(Util.dist(x, y, e.getX(), e.getY()) < Util.dist(x, y, target.getX(), target.getY())){
                    target = e;
                }
            }
        }
        damageRandom = new Random();
        double e = Math.pow(getStrength(claw), (Math.cbrt(damageRandom.nextInt(15)) + 1)) / (Math.sqrt(getStrength(claw) + 1));
        target.damage((int) Math.round(e));
        Util.writeLine(" : " + target.getCurrentHealth() + "/" + target.getMaxHP());
        if(target.getCurrentHealth() <= 0){
            addXP(100);
        }
    }

    delay.start();
}
private void outputData(boolean ask){
    if(ask){
        for(int i = 0; i < inv.getLength(); i++){
            Util.writeLine("Main Inv Slot "+i+": " + inv.get(i));
        }
        Util.writeLine("");
        Util.writeLine("Backpack Owned: " + getBackpack());
        Util.writeLine("");
        Util.writeLine("Inventory Size: " + inv.getLength());
        Util.writeLine("");
        Util.writeLine("Health: "+getCurrentHealth());
        Util.writeLine("");
        Util.writeLine("End of Information Output");
    }
}
}

Class add()在:

中定义
package geniushour.game.inventory;

import geniushour.game.Util;
import geniushour.gameobject.item.Item;

public class Inventory {

private Item[] items;
private int firstFree;

public Inventory(int size){
    items = new Item[size];
    firstFree = 0;
}
public boolean add(Item item){
    for(int i = 0; i < items.length; i++){
        if(get(i) == null){
            firstFree = i;
            break;
        }
    }
    Util.writeLine(firstFree);
    if(add(item, firstFree)){
        firstFree++;
        if(firstFree > items.length){
            firstFree = items.length;
        }
        return true;
    }
    return false;
}
public boolean add(Item item, int index){
    if(items[index] == null && index <= items.length){ // != or == ?
        items[index] = item;
        return true;
    }
    else{
        return false;
    }
}
public Item get(int index){
    return items[index];
}
public void remove(int index){
    items[index] = null;
    if(index < firstFree){
        firstFree = index;
    }
    if(firstFree < 0){
        firstFree = 0;
    }
}
public void remove(Item item){
    for(int i = 0; i < items.length; i++){
        if(items[i] == item){
            remove(i);
            return;
        }
    }
}
public Item[] getItemList(){
    return items;
}
public int getLength(){
    return items.length;
}
public void setSlots(int amt){
    Item[] temp = new Item[items.length];
    for(int i = 0; i < items.length; i++){
        temp[i] = items[i];
    }
    items = new Item[amt];
    for(int i = 0; i < temp.length; i++){
        items[i] = temp[i];
    }
    temp = new Item[items.length];
}


}

0 个答案:

没有答案