Ok so I'm making sort-of a Dungeon Crawler and the thing is how do i remember the current HP of the player and not reinitialize it every time I enter a new world???
public void addPlayer(Player player) { addObject(player, 50, 300); // wherever }
World2 w2 = new World2(); w2.addPlayer(this); Greenfoot.setWorld(w2);
public boolean attack() { if (Greenfoot.isKeyDown("x")) { if (!getWorld().getObjects(EnemyTest.class).isEmpty()) {snake = getWorld().getObjects(EnemyTest.class).get(0); if (snake!=null && isInRange()) { if ((direction == 1 && getX()<=snake.getX()) || (direction == -1 && getX()>=snake.getX())) getWorld().removeObject(snake); } } if (isAttacking==true) return false; return true; } return false; }
getWorld().getObjects(Enemy.class).get(0).HP--;
( (Enemy) getWorld().getObjects(Enemy.class).get(0) ).HP--;
( (Enemy) getWorld().getObjects(Enemy.class).get(0) ).HP--;
public boolean attack() { if (Greenfoot.isKeyDown("x")) { if (!getWorld().getObjects(Enemy.class).isEmpty()) {enemy = getWorld().getObjects(Enemy.class).get(0); if (enemy!=null && isInRange()) { if ((direction == 1 && getX()<=enemy.getX()) || (direction == -1 && getX()>=enemy.getX())) ( (Enemy) getWorld().getObjects(Enemy.class).get(0)).HP--; //dealing damage } } if (isAttacking==true) return false; return true; } return false; }
public int HP; public void act() { // Add your action code here. }
if (isSpawned==false) {isSpawned=true; HP=1; return;} if (HP==0) die(); else ....//code for attacking and tracking the player
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class SnakeEnemy here. * * @author (your name) * @version (a version number or a date) */ public class SnakeEnemy extends Enemy { private final GreenfootImage[] MoveRightAnimation = { new GreenfootImage("Snake_Move_1.png"), new GreenfootImage("Snake_Move_2.png"), new GreenfootImage("Snake_Move_3.png"), new GreenfootImage("Snake_Move_4.png"), new GreenfootImage("Snake_Move_5.png") }; private final GreenfootImage[] MoveLeftAnimation = { new GreenfootImage("LSnake_Move_1.png"), new GreenfootImage("LSnake_Move_2.png"), new GreenfootImage("LSnake_Move_3.png"), new GreenfootImage("LSnake_Move_4.png"), new GreenfootImage("LSnake_Move_5.png") }; private final GreenfootImage[] AttackRightAnimation = { new GreenfootImage("Snake_Attack_1.png"), new GreenfootImage("Snake_Attack_2.png"), new GreenfootImage("Snake_Attack_3.png"), new GreenfootImage("Snake_Attack_4.png"), new GreenfootImage("Snake_Attack_5.png"), new GreenfootImage("Snake_Attack_6.png"), new GreenfootImage("Snake_Attack_7.png"), new GreenfootImage("Snake_Attack_8.png"), new GreenfootImage("Snake_Attack_9.png") }; private final GreenfootImage[] AttackLeftAnimation = { new GreenfootImage("LSnake_Attack_1.png"), new GreenfootImage("LSnake_Attack_2.png"), new GreenfootImage("LSnake_Attack_3.png"), new GreenfootImage("LSnake_Attack_4.png"), new GreenfootImage("LSnake_Attack_5.png"), new GreenfootImage("LSnake_Attack_6.png"), new GreenfootImage("LSnake_Attack_7.png"), new GreenfootImage("LSnake_Attack_8.png"), new GreenfootImage("LSnake_Attack_9.png") }; private final GreenfootImage[] DieRightAnimation = { new GreenfootImage("Snake_Die_1.png"), new GreenfootImage("Snake_Die_2.png"), new GreenfootImage("Snake_Die_3.png"), new GreenfootImage("Snake_Die_4.png"), new GreenfootImage("Snake_Die_5.png"), new GreenfootImage("Snake_Die_6.png") }; private final GreenfootImage[] DieLeftAnimation = { new GreenfootImage("LSnake_Die_1.png"), new GreenfootImage("LSnake_Die_2.png"), new GreenfootImage("LSnake_Die_3.png"), new GreenfootImage("LSnake_Die_4.png"), new GreenfootImage("LSnake_Die_5.png"), new GreenfootImage("LSnake_Die_6.png") }; public int direction = 1; public int dy=1; public int distx; public int disty; public int frame=-1; public int speed= 2; public boolean isAttacking=false; public boolean isSpawned=false; public int range=40; public int attackDelay=-1; Actor wall; Actor othersnek; public void act() { // Add your action code here. if (isSpawned==false) {isSpawned=true; HP=1; return;} if (HP==0) die(); else{ if (getWorld().getObjects(Player.class).isEmpty() || getWorld().getObjects(Player.class).get(0).HP==0) return; if (attackDelay!=-1) delayAttack(); else if (isInRange() && isAttacking==false && attackDelay==-1) {frame=0; isAttacking=true; } else if (isAttacking==true) {attack(); if (frame==15 && getWorld().getObjects(HP_HUD.class).get(0).returnHP()>0){ getWorld().getObjects(HP_HUD.class).get(0).HP--; getWorld().getObjects(Player.class).get(0).isHit=true; } else if (frame==44) {frame=-1; isAttacking=false; getWorld().getObjects(Player.class).get(0).isHit=false; attackDelay=0; delayAttack(); }} else { checkmove(); move(); }} } public void checkmove() { Actor player =getWorld().getObjects(Player.class).get(0); if (player.getX()>getX()) direction = 1; else if (player.getX()<getX()) direction =-1; if (player.getY()>getY()) dy=1; else dy=-1; if (direction == 1 ) {animateMoveRight();} else animateMoveLeft(); if(player.getX()==getX()) direction=0; } public boolean isInRange() { Actor player = getWorld().getObjects(Player.class).get(0); if (player.getX()>getX()) distx=player.getX()-getX(); else distx=getX()-player.getX(); if (player.getY()>getY()) disty=player.getY()-getY(); else disty=getY()-player.getY(); if (distx<range && disty<range) return true; return false; } public void move() { Actor player =getWorld().getObjects(Player.class).get(0); wall = getOneObjectAtOffset(0,-10,Wall1.class); if (wall==null || getY()<player.getY()) setLocation (getX() + direction*speed,getY()+dy*speed); } public void attack() { if (direction==1) animateAttackRight(); else animateAttackLeft(); } public void die() { if (direction == 1 ) animateDieRight(); else animateDieLeft(); } public void delayAttack() {attackDelay++; if(attackDelay==30) attackDelay=-1; } public void animateMoveRight() { frame++; setImage(MoveRightAnimation[frame/10]); if (frame==49) frame=-1; } public void animateMoveLeft() { frame++; setImage(MoveLeftAnimation[frame/10]); if (frame==49) frame=-1; } public void animateAttackRight() { frame++; setImage(AttackRightAnimation[frame/5]); if (frame==44) {frame=-1;isAttacking=false; getWorld().getObjects(Player.class).get(0).isHit=false; attackDelay=0; delayAttack();} } public void animateAttackLeft() { frame++; setImage(AttackLeftAnimation[frame/5]); if (frame==44) {frame=-1;isAttacking=false; getWorld().getObjects(Player.class).get(0).isHit=false; attackDelay=0; delayAttack();} } public void animateDieRight() { if (frame<59) frame++; setImage(DieRightAnimation[frame/10]); } public void animateDieLeft() { if (frame<59) frame++; setImage(DieLeftAnimation[frame/10]); } }
public SnakeEnemy() { HP = 1; }
public SnakeEnemy() { HP = 1; }
java.util.List enemies = getWorld().getObjects(Enemy.class); if (!enemies.isEmpty()) { for (Object obj : enemies) { Enemy enemy = (Enemy)obj; if (....) enemy.HP--; } return !isAttacking; }