Disclaimer: I am german, you can answer in german. I also use a lot of public variables and no getter and setter, but "((MyWorld)getWorld()).variable" instead, because it is simpler for me right now. I know it's bad. (If it makes my whole game easier you can also give me tips on how to use them in this scenario, but it's not my question here).
Heyho Greenfoot Community, I need help with tracking 2 interacting actors and change variables of them.
/////////////I will be marking specific question by commenting them like this/////////////
I am currently working on a Top Down shooter and I am currently facing the challenge of programming variable penetrating bullets.
I have my character "joeMama"(joeMama.class), who shoots "e1" (e1.class, meaning enemy1) with "ball" (ball.class).
joeMama will try to shoot the "e1" by spawning "ball" with a specific cooldown on left click (already working).
The ball will do the following on collision:
What I am trying to achieve is, that not every act-cycle both parties are reduced by 1 healthpoint, but in an instance, so the "ball" will further move after reducing the "e1's" health to zero and only after the clash has happened and not while it is happening.
When only dealing one damage the do-while is working, but as soon, as I add damage it gets weird. Adding damage in this sense means adding a point to bulletHP. The do-while will act one more time for each bulletHP, so it makes sense.
What happens though is, that other instances than the "e1" that is shoot will be taking damage.
/////////////How can I make the "ball" make a list of nearby "e1" on contact with one, change the variable "e1Health" of the first "e1" in the list, depending on range, and delete the list upon making the "e1" disappear.////////////////
I am also welcoming alternatives, but I would have thought of this as one of the best solutions.
Code of the "e1":
My best try is something like this, but I couldn't find how to code it:
I will probably edit things, please ask if you need more to know more.
Thanks for answers in advance!
public class ball extends Actor { /** * Act - do whatever the ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private boolean toBeDeleted=false; private int bulletMoveSpeed=25; public int initialBulletHP=1,bulletHP; private int hitboxE1=40; int i=1; public ball(){ } public void act() { for(int i=1;i==1;i++){ initialBulletHP=((MyWorld)getWorld()).buDa; bulletHP=initialBulletHP; } if(((MyWorld)getWorld()).wIP==false){ move(bulletMoveSpeed); } if(this.isAtEdge()&&this!=null)this.toBeDeleted=true; if(this.toBeDeleted==true){ getWorld().removeObject(this); i++; } if(i==1){ Actor intObj = getOneIntersectingObject(e1.class); if(intObj!=null){ do{ Actor intObj2 = getOneIntersectingObject(e1.class); this.bulletHP--; ((MyWorld)getWorld()).getObjects(e1.class).get(0).e1Health--; if(((MyWorld)getWorld()).getObjects(e1.class).get(0).e1Health<=0)break; if(this.bulletHP==0)break; }while(this.bulletHP>0); } if(this.bulletHP<=0)this.toBeDeleted=true; } } }
public class e1 extends enemies { private int range = 2000, moveSpeed=1; public int e1Damage=12, e1Health=3, e1Worth=10; public int e1XPgivings; //how much XP u gain public int spawnImmunity=10; public boolean toBeDeleted=false; GreenfootImage lobster = new GreenfootImage("lobster.png"); public void e1(){ setImage(lobster); } public void act() { if(spawnImmunity>0)spawnImmunity--; if(((MyWorld)getWorld()).wIP==false){ //important for pause, almost every actor and the world has most methods only active when the game isn't paused movement(); damageCalc(); } if(this.toBeDeleted==true)this.getWorld().removeObject(this); } public void movement(){ if (!getObjectsInRange( range, joeMama.class ).isEmpty()){ Actor jM = getObjectsInRange(range,joeMama.class ).get(0); turnTowards( jM.getX(), jM.getY()); } move(this.moveSpeed); Actor bullet = getOneIntersectingObject(ball.class); Actor player = getOneIntersectingObject(joeMama.class); if(player != null){ ((MyWorld)getWorld()).playerHealth-=e1Damage+e1Health*2; ((MyWorld)getWorld()).regCd=((MyWorld)getWorld()).regMaxCd; //irrelevant this.toBeDeleted=true; } } public void damageCalc(){ if(spawnImmunity<=0){ this.e1XPgivings=Greenfoot.getRandomNumber(6)+4; if(this.e1Health<=0||this.toBeDeleted==true){ ((MyWorld)getWorld()).scorePoints+=e1Worth; ((MyWorld)getWorld()).pcX+=e1XPgivings; this.toBeDeleted=true; } } } }
Actor ne1 = getObjectsInRange(exampleSizeOfE1,e1.class ).get(0); //nearest enemy in range //then something like this: if(ne1!=null){ ((MyWorld)getWorld()).getActor(ne1).e1Health--; }