Enemy ai is basic but functional

master
Jonathan Hager 5 years ago
parent ec241d3d53
commit 85164681f2

@ -5,15 +5,89 @@ import com.badlogic.gdx.math.Vector2;
public class Enemy extends FightObject{
boolean move = false;
boolean isMelee;
public Enemy(float x, float y, AnimatedSprite sprite, Stats stats, int id) {
public Enemy(float x, float y, AnimatedSprite sprite, Stats stats, int id, boolean isMelee) {
super(x, y, sprite, stats, id);
this.isMelee = isMelee;
}
public void act(){
public void act(FightPlayer player){
if(POI == null && !move){
POI = new Vector2(x, y - 32);
move = true;
double distance = StaticMath.calculateDistance(x, y, player.x, player.y);
if(isMelee) {
if(distance <= 32f) {
System.out.println("Jetzt stirbst du *kawumm*");
attack(player);
moves--;
state = 2;
}
else {
float tempX = x;
float tempY = y;
float deltaX = player.x - x;
float deltaY = player.y - y;
if(Math.abs(deltaX) >= Math.abs(deltaY)) {
tempX += (deltaX / Math.abs(deltaX)) * 32;
}
else {
tempY += (deltaY / Math.abs(deltaY)) * 32;
}
POI = new Vector2(tempX, tempY);
moves--;
move = true;
}
}
else {
if(distance >= 96f && distance <= 150f) {
System.out.println("Wilhelm Tell is ein Scheiß gegen mich *surr*");
attack(player);
moves--;
state = 2;
}
else if(distance < 96f){
float tempX = x;
float tempY = y;
float deltaX = player.x - x;
float deltaY = player.y - y;
if(Math.abs(deltaX) >= Math.abs(deltaY)) {
tempX += -(deltaX / Math.abs(deltaX)) * 32;
}
else {
tempY += -(deltaY / Math.abs(deltaY)) * 32;
}
POI = new Vector2(tempX, tempY);
moves--;
move = true;
}
else {
float tempX = x;
float tempY = y;
float deltaX = player.x - x;
float deltaY = player.y - y;
if(Math.abs(deltaX) >= Math.abs(deltaY)) {
tempX += (deltaX / Math.abs(deltaX)) * 32;
}
else {
tempY += (deltaY / Math.abs(deltaY)) * 32;
}
POI = new Vector2(tempX, tempY);
moves--;
move = true;
}
}
}
else if(move && POI == null){
state = 2;

@ -6,28 +6,43 @@ public abstract class FightObject {
protected AnimatedSprite sprite;
protected Stats stats;
protected int id;
protected float x;
protected float y;
protected Vector2 POI;
// 0: waiting 1: doing action 2: finished action
protected int state = 0;
protected float x;
protected float y;
protected Vector2 POI;
protected int moves;
protected int maxMoves;
// 0: waiting 1: doing action 2: finished action
protected int state = 0;
public FightObject(float x, float y, AnimatedSprite sprite, Stats stats, int id) {
this.sprite = sprite;
this.stats = stats;
this.id = id;
this.x = x;
this.y = y;
this.x = x;
this.y = y;
maxMoves = 2;
moves = maxMoves;
}
void setX(float x) {
this.x = x;
this.sprite.setSpritePosition((int)this.x, (int)this.y);
}
void setY(float y) {
this.y = y;
this.sprite.setSpritePosition((int)this.x, (int)this.y);
}
void setX(float x) {
this.x = x;
this.sprite.setSpritePosition((int)this.x, (int)this.y);
}
void setY(float y) {
this.y = y;
this.sprite.setSpritePosition((int)this.x, (int)this.y);
}
void attack(FightObject o) {
if(o != null) {
o.stats.setHp(o.stats.getHp() - stats.getAtk());
}
}
public void startAction() {
state = 1;
moves = maxMoves;
}
}

@ -50,7 +50,7 @@ public class FightScreen {
}
}
}
objects[0].state = 1;
objects[0].startAction();
}
public void act(float deltatime) {
@ -107,16 +107,21 @@ public class FightScreen {
}
}
else if(objects[i] instanceof Enemy){
((Enemy)objects[i]).act();
for(FightObject player : objects) {
if(player instanceof FightPlayer) {
((Enemy)objects[i]).act((FightPlayer) player);
break;
}
}
}
}
else if(objects[i].state == 2){
objects[i].state = 0;
if(i == objects.length-1){
objects[0].state = 1;
objects[0].startAction();
}
else{
objects[i+1].state = 1;
objects[i+1].startAction();
}
}
}
@ -186,7 +191,7 @@ public class FightScreen {
return (FightPlayer) object;
}
}
System.out.println("großes Problem hahgaeu9ihgbidesrufhgred");
System.out.println("gro<EFBFBD>es Problem hahgaeu9ihgbidesrufhgred");
return null;
}

@ -17,14 +17,16 @@ public class Hostile extends Actor {
Rectangle collisionRect;
Circle attackCircle;
Circle attentionCircle;
boolean isMelee;
// 0: normal movement, 1: locked onto Player, 2: attacking
int movementState;
public Hostile(float xPos, float yPos, int id, Stats stats, String texture) {
public Hostile(float xPos, float yPos, int id, Stats stats, String texture, boolean isMelee) {
this.id = id;
this.stats = stats;
this.isMelee = isMelee;
Texture tx = new Texture(Gdx.files.internal("textureData/sprites/" + texture));
sprite = new AnimatedSprite(tx, 64, 64, true);

@ -156,8 +156,8 @@ public class MapContainer {
stage.addActor(p);
stage.addActor(new Hostile(200, 200, 0, new Stats(), "sprite.png"));
stage.addActor(new Hostile(265, 200, 1, new Stats(), "sprite.png"));
stage.addActor(new Hostile(200, 200, 0, new Stats(), "sprite.png", false));
stage.addActor(new Hostile(265, 200, 1, new Stats(), "sprite.png", true));
}
public void render(float f){
@ -186,7 +186,7 @@ public class MapContainer {
if(a instanceof Hostile) {
if(((Hostile) a).movementState > 0) {
((Hostile) a).movementState = 2;
Enemy e = new Enemy(a.getX(), a.getY(), ((Hostile) a).sprite, ((Hostile) a).stats, ((Hostile) a).id);
Enemy e = new Enemy(a.getX(), a.getY(), ((Hostile) a).sprite, ((Hostile) a).stats, ((Hostile) a).id, ((Hostile) a).isMelee);
tempObjects.add(e);
}
}
@ -254,7 +254,7 @@ public class MapContainer {
for(Actor a : stage.getActors()) {
if(a instanceof Hostile) {
Enemy e = new Enemy(a.getX(), a.getY(), ((Hostile) a).sprite, ((Hostile) a).stats, ((Hostile) a).id);
Enemy e = new Enemy(a.getX(), a.getY(), ((Hostile) a).sprite, ((Hostile) a).stats, ((Hostile) a).id, ((Hostile) a).isMelee);
tempObjects.add(e);
}
}

Loading…
Cancel
Save