Gegner können sich jetzt bewegen

This commit is contained in:
2026-09-16 16:21:57 +02:00
parent 8477d63e51
commit c605600c84
4 changed files with 112 additions and 68 deletions
+24
View File
@@ -2,12 +2,36 @@ import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Enemy extends Actor
{
int targetX;
int targetY;
boolean targetReached = false;
int speed = 3;
public Enemy (int targetX, int targetY) {
this.targetX = targetX;
this.targetY = targetY;
}
int shootTimer;
public void act()
{
shootAtPlayer();
if (!targetReached){
moveToTargetLocation(targetX, targetY);
}
}
public void moveToTargetLocation(int targetX, int targetY){
double dx = targetX - getX();
double dy = targetY - getY();
double distance = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
if (distance < 2){
targetReached = true;
}
setLocation(getX() + (int)(dx/distance*speed), getY() + (int)(dy/distance*speed));
}
public void shootAtPlayer(){
shootTimer++;