From 29d35fc1eb1699d9757d4528f00254ff06406263 Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Fri, 1 May 2020 12:54:38 +0200 Subject: [PATCH] Gegner laufen auf Player zu --- .../dungeoncrawler/control/Controller.java | 5 +- core/src/com/dungeoncrawler/model/Entity.java | 48 +++++----------- .../dungeoncrawler/model/entities/Archer.java | 17 ++++-- .../dungeoncrawler/model/entities/Arrow.java | 5 ++ .../dungeoncrawler/model/entities/Player.java | 5 ++ .../model/entities/Swordsman.java | 55 +++++++++++++++++-- .../com/dungeoncrawler/view/GameScreen.java | 10 ++-- 7 files changed, 93 insertions(+), 52 deletions(-) diff --git a/core/src/com/dungeoncrawler/control/Controller.java b/core/src/com/dungeoncrawler/control/Controller.java index 921b460..4ee8893 100644 --- a/core/src/com/dungeoncrawler/control/Controller.java +++ b/core/src/com/dungeoncrawler/control/Controller.java @@ -104,11 +104,12 @@ public class Controller extends ApplicationAdapter implements InputProcessor{ public void run() { for(int i = 0; i < d.getCurrentEntities().length; i++){ if(d.getCurrentEntities()[i] != null){ - d.getCurrentEntities()[i].randomMove(roomX, roomY); + //d.getCurrentEntities()[i].randomMove(roomX, roomY); + d.getCurrentEntities()[i].move((int) d.getPlayer().getxPos(), (int) d.getPlayer().getyPos()); } } } - },0,1f); + },0, 0.03f); } diff --git a/core/src/com/dungeoncrawler/model/Entity.java b/core/src/com/dungeoncrawler/model/Entity.java index 3c68e4d..059a4b7 100644 --- a/core/src/com/dungeoncrawler/model/Entity.java +++ b/core/src/com/dungeoncrawler/model/Entity.java @@ -1,8 +1,5 @@ package com.dungeoncrawler.model; -import com.badlogic.gdx.utils.Timer; - - public abstract class Entity { protected float xPos; @@ -14,7 +11,7 @@ public abstract class Entity { protected float movementX; protected float movementY; protected int id; - protected int facing; + protected int direction; protected Inventory inv; @@ -25,16 +22,9 @@ public abstract class Entity { this.lvl = lvl; this.movementX = 0; this.movementY = 0; - this.facing = 2; - - - - - + this.direction = 2; } - - public void attack(){ } @@ -62,24 +52,13 @@ public abstract class Entity { movementY = 0; } - public void rdmMove(){ - - } - - public int direction(){ // returns direction the entity is facing depending on its movement - if(movementX == -3f){ // TIS IS SHIT - NEED REWORK - facing = 3; - } - else if(movementX == 3f){ - facing = 1; - } - else if(movementY == 3f){ - facing = 0; + public void updateDirection(){ + if(movementX > 1){ + direction = 1; } - else if(movementY == -3f){ - facing = 2; + else if(movementX < -1){ + direction = 0; } - return facing; } @@ -152,15 +131,18 @@ public abstract class Entity { return this.id; } - public int getFacing(){ - return facing; + public int getDirection(){ + return direction; } - public void setFacing(int facing){ - this.facing = facing; + + public void setDirection(int direction){ + this.direction = direction; } + public void randomMove(int x, int y){ + } - + abstract public void move(int xPosPlayer, int yPosPlayer); } \ No newline at end of file diff --git a/core/src/com/dungeoncrawler/model/entities/Archer.java b/core/src/com/dungeoncrawler/model/entities/Archer.java index 32490df..ee2bf25 100644 --- a/core/src/com/dungeoncrawler/model/entities/Archer.java +++ b/core/src/com/dungeoncrawler/model/entities/Archer.java @@ -17,7 +17,7 @@ public class Archer extends Entity{ this.maxhp = 5*lvl; this.hp = this.maxhp; - this.facing = 2; + this.direction = 2; this.dmg = 3*lvl; this.id = 0; // TODO: Sinnvolle Werte finden @@ -28,12 +28,12 @@ public class Archer extends Entity{ tleft = new Timer(); isRunning = false; timerRuns = 0; - facing = 2; + direction = 2; tup.scheduleTask(new Timer.Task() { @Override public void run() { - setFacing(0); + setDirection(0); setyPos(getyPos() + 1f); setTimerRuns(getTimerRuns() + 1); if(getTimerRuns() >= 48){ @@ -47,7 +47,7 @@ public class Archer extends Entity{ tright.scheduleTask(new Timer.Task() { @Override public void run() { - setFacing(1); + setDirection(1); setxPos(getxPos() + 1f); setTimerRuns(getTimerRuns() + 1); if(getTimerRuns() >= 48){ @@ -61,7 +61,7 @@ public class Archer extends Entity{ tdown.scheduleTask(new Timer.Task() { @Override public void run() { - setFacing(2); + setDirection(2); setyPos(getyPos() - 1f); setTimerRuns(getTimerRuns() + 1); if(getTimerRuns() >= 48){ @@ -75,7 +75,7 @@ public class Archer extends Entity{ tleft.scheduleTask(new Timer.Task() { @Override public void run() { - setFacing(3); + setDirection(3); setxPos(getxPos() - 1f); setTimerRuns(getTimerRuns() + 1); @@ -152,6 +152,11 @@ public class Archer extends Entity{ public void setTimerRuns(int n){ timerRuns = n; } + + @Override + public void move(int xPosPlayer, int yPosPlayer) { + // Nothing + } diff --git a/core/src/com/dungeoncrawler/model/entities/Arrow.java b/core/src/com/dungeoncrawler/model/entities/Arrow.java index a40ade0..698bacb 100644 --- a/core/src/com/dungeoncrawler/model/entities/Arrow.java +++ b/core/src/com/dungeoncrawler/model/entities/Arrow.java @@ -84,4 +84,9 @@ public class Arrow extends Entity{ public float getyStart(){ return yStart; } + + @Override + public void move(int xPosPlayer, int yPosPlayer) { + // Nothing + } } diff --git a/core/src/com/dungeoncrawler/model/entities/Player.java b/core/src/com/dungeoncrawler/model/entities/Player.java index 4e9c7d7..91f0359 100644 --- a/core/src/com/dungeoncrawler/model/entities/Player.java +++ b/core/src/com/dungeoncrawler/model/entities/Player.java @@ -34,5 +34,10 @@ public class Player extends Entity { public Inventory getInv(){ return inv; } + + @Override + public void move(int xPosPlayer, int yPosPlayer) { + // Nothing + } } diff --git a/core/src/com/dungeoncrawler/model/entities/Swordsman.java b/core/src/com/dungeoncrawler/model/entities/Swordsman.java index 4eaf03a..14e2ed2 100644 --- a/core/src/com/dungeoncrawler/model/entities/Swordsman.java +++ b/core/src/com/dungeoncrawler/model/entities/Swordsman.java @@ -16,7 +16,7 @@ public class Swordsman extends Entity { this.maxhp = 5*lvl; this.hp = this.maxhp; - this.facing = 2; + this.direction = 2; this.dmg = 3*lvl; this.id = 1; // TODO: Sinnvolle Werte finden @@ -26,13 +26,13 @@ public class Swordsman extends Entity { tleft = new Timer(); isRunning = false; timerRuns = 0; - facing = 2; + direction = 2; tup.scheduleTask(new Timer.Task() { @Override public void run() { - setFacing(0); + setDirection(0); setyPos(getyPos() + 1f); setTimerRuns(getTimerRuns() + 1); if(getTimerRuns() >= 48){ @@ -46,7 +46,7 @@ public class Swordsman extends Entity { tright.scheduleTask(new Timer.Task() { @Override public void run() { - setFacing(1); + setDirection(1); setxPos(getxPos() + 1f); setTimerRuns(getTimerRuns() + 1); if(getTimerRuns() >= 48){ @@ -60,7 +60,7 @@ public class Swordsman extends Entity { tdown.scheduleTask(new Timer.Task() { @Override public void run() { - setFacing(2); + setDirection(2); setyPos(getyPos() - 1f); setTimerRuns(getTimerRuns() + 1); if(getTimerRuns() >= 48){ @@ -74,7 +74,7 @@ public class Swordsman extends Entity { tleft.scheduleTask(new Timer.Task() { @Override public void run() { - setFacing(3); + setDirection(3); setxPos(getxPos() - 1f); setTimerRuns(getTimerRuns() + 1); @@ -148,5 +148,48 @@ public class Swordsman extends Entity { timerRuns = n; } + @Override + public void move(int xPosPlayer, int yPosPlayer){ + int deltaX = xPosPlayer - (int) xPos; + int deltaY = yPosPlayer - (int) yPos; + + double alpha; + if(deltaX == 0 && deltaY >= 0){ + alpha = Math.PI / 2; + } + else if(deltaX == 0 && deltaY < 0){ + alpha = Math.PI / -2; + } + else{ + alpha = Math.abs(Math.atan(deltaY / deltaX)); + + if(deltaX < 0 && deltaY < 0){ + alpha = Math.PI + alpha; + } + else if(deltaX < 0 && deltaY > 0){ + alpha = Math.PI - alpha; + } + else if(deltaX > 0 && deltaY < 0){ + alpha = 2*Math.PI - alpha; + } + } + + movementX = (int) (3 * Math.cos(alpha)); + movementY = (int) (3 * Math.sin(alpha)); + + /* + if(deltaX < 0 || deltaY < 0){ + movementX *= -1; + movementY *= -1; + } + else if(deltaY < 0){ + movementX *= -1; + } + */ + + xPos += movementX; + yPos += movementY; + } + } diff --git a/core/src/com/dungeoncrawler/view/GameScreen.java b/core/src/com/dungeoncrawler/view/GameScreen.java index bb0a98c..b048009 100644 --- a/core/src/com/dungeoncrawler/view/GameScreen.java +++ b/core/src/com/dungeoncrawler/view/GameScreen.java @@ -279,7 +279,7 @@ public class GameScreen { //DRAW'T JEDES ENTITY - prueft vorher ob vorhanden for(int i = 0; i < e.length; i++){ if(e[i] != null){ - switch(e[i].getFacing()){ + switch(e[i].getDirection()){ case -1: break; case 0: @@ -355,7 +355,7 @@ public class GameScreen { public Entity[] playerAttack(Entity e[], Player p, SpriteBatch batch){ - if(p.direction() == 0){ + if(p.getDirection() == 0){ Texture attackTexture = new Texture("sprites/AttackHori.png"); Sprite attackSprite = new Sprite(attackTexture); attackSprite.setX(p.getxPos() - 8f); @@ -376,7 +376,7 @@ public class GameScreen { } } } - else if(p.direction() == 1){ + else if(p.getDirection()== 1){ Texture attackTexture = new Texture("sprites/AttackVert.png"); Sprite attackSprite = new Sprite(attackTexture); attackSprite.setX(p.getxPos()+ 32f); @@ -396,7 +396,7 @@ public class GameScreen { } } } - else if(p.direction() == 2){ + else if(p.getDirection()== 2){ Texture attackTexture = new Texture("sprites/AttackHori.png"); Sprite attackSprite = new Sprite(attackTexture); attackSprite.setX(p.getxPos() - 8f); @@ -416,7 +416,7 @@ public class GameScreen { } } } - else if(p.direction() == 3){ + else if(p.getDirection()== 3){ Texture attackTexture = new Texture("sprites/AttackVert.png"); Sprite attackSprite = new Sprite(attackTexture); attackSprite.setX(p.getxPos() - 32f);