diff --git a/core/src/com/dungeoncrawler/control/Controller.java b/core/src/com/dungeoncrawler/control/Controller.java index 9ffc22f..5e20ea7 100644 --- a/core/src/com/dungeoncrawler/control/Controller.java +++ b/core/src/com/dungeoncrawler/control/Controller.java @@ -13,6 +13,7 @@ import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.maps.MapLayers; +import com.badlogic.gdx.maps.MapObject; import com.badlogic.gdx.maps.MapObjects; import com.badlogic.gdx.maps.objects.RectangleMapObject; import com.badlogic.gdx.math.Intersector; @@ -103,11 +104,44 @@ 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); - } + if(m != null){ + // Gets the collisions relevant sprites + MapObjects mapObjects = m.getM().getMaps()[level][roomPosX][roomPosY].getMap().getLayers().get(0).getObjects(); + Rectangle playerSprite = m.getPlayer().getCollisionSprite(); + + Entity temp = d.getCurrentEntities()[i]; + + int x = (int) temp.getxPos(); + int y = (int) temp.getyPos(); + + d.getCurrentEntities()[i].move((int) d.getPlayer().getxPos(), (int) d.getPlayer().getyPos()); + Sprite tempObject = m.entitySprites[i]; + + + boolean overlaps = false; + if(Intersector.overlaps(tempObject.getBoundingRectangle(), playerSprite)){ + overlaps = true; + } + else{ + for(RectangleMapObject rectangleObject : mapObjects.getByType(RectangleMapObject.class)){ + Rectangle rectangle = rectangleObject.getRectangle(); + + if(Intersector.overlaps(tempObject.getBoundingRectangle(), rectangle)){ + overlaps = true; + break; + } + } + } + + if(overlaps){ + d.getCurrentEntities()[i].setxPos(x); + d.getCurrentEntities()[i].setyPos(y); + } + } + } } } - },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..80b4d17 100644 --- a/core/src/com/dungeoncrawler/model/Entity.java +++ b/core/src/com/dungeoncrawler/model/Entity.java @@ -1,7 +1,8 @@ package com.dungeoncrawler.model; -import com.badlogic.gdx.utils.Timer; - +import com.badlogic.gdx.maps.MapObject; +import com.badlogic.gdx.maps.MapObjects; +import com.badlogic.gdx.math.Rectangle; public abstract class Entity { @@ -14,7 +15,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 +26,9 @@ public abstract class Entity { this.lvl = lvl; this.movementX = 0; this.movementY = 0; - this.facing = 2; - - - - - + this.direction = 2; } - - public void attack(){ } @@ -62,26 +56,16 @@ 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; } + abstract public void move(int xPosPlayer, int yPosPlayer); // GETTER + SETTER public float getxPos() { @@ -152,15 +136,12 @@ public abstract class Entity { return this.id; } - public int getFacing(){ - return facing; - } - public void setFacing(int facing){ - this.facing = facing; - } - public void randomMove(int x, int y){ + public int getDirection(){ + return direction; } - + public void setDirection(int direction){ + this.direction = direction; + } } \ 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..f36509c 100644 --- a/core/src/com/dungeoncrawler/model/entities/Archer.java +++ b/core/src/com/dungeoncrawler/model/entities/Archer.java @@ -1,156 +1,26 @@ package com.dungeoncrawler.model.entities; import com.dungeoncrawler.model.Entity; -import com.badlogic.gdx.utils.Timer; public class Archer extends Entity{ - Timer tup; - Timer tright; - Timer tdown; - Timer tleft; - int timerRuns; - boolean isRunning; - public Archer(float xPos, float yPos, int lvl) { super(xPos, yPos, lvl); 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 - tup = new Timer(); - tright = new Timer(); - tdown = new Timer(); - tleft = new Timer(); - isRunning = false; - timerRuns = 0; - facing = 2; - - tup.scheduleTask(new Timer.Task() { - @Override - public void run() { - setFacing(0); - setyPos(getyPos() + 1f); - setTimerRuns(getTimerRuns() + 1); - if(getTimerRuns() >= 48){ - setTimerRuns(0); - setIsRunning(false); - tup.stop(); - } - } - },0,0.015f); - tup.stop(); - tright.scheduleTask(new Timer.Task() { - @Override - public void run() { - setFacing(1); - setxPos(getxPos() + 1f); - setTimerRuns(getTimerRuns() + 1); - if(getTimerRuns() >= 48){ - setTimerRuns(0); - setIsRunning(false); - tright.stop(); - } - } - },0,0.015f); - tright.stop(); - tdown.scheduleTask(new Timer.Task() { - @Override - public void run() { - setFacing(2); - setyPos(getyPos() - 1f); - setTimerRuns(getTimerRuns() + 1); - if(getTimerRuns() >= 48){ - setTimerRuns(0); - setIsRunning(false); - tdown.stop(); - } - } - },0,0.015f); - tdown.stop(); - tleft.scheduleTask(new Timer.Task() { - @Override - public void run() { - setFacing(3); - setxPos(getxPos() - 1f); - setTimerRuns(getTimerRuns() + 1); - - if(getTimerRuns() >= 48){ - setTimerRuns(0); - setIsRunning(false); - tleft.stop(); - } - } - },0,0.015f); - tleft.stop(); + direction = 2; } - + @Override - public void randomMove(int x, int y) { - double i = Math.random(); - if(i <= 0.2){ - if(isRunning == false){ - if(getyPos() >= (x-1) * 48f){ - } - else{ - setIsRunning(true); - tup.start(); - } - } - } - else if(i > 0.2 && i <= 0.4){ - if(isRunning == false){ - if(getxPos() >= (y-1) * 48f){ - } - else{ - setIsRunning(true); - tright.start(); - } - } - } - else if(i > 0.4 && i <= 0.6){ - if(isRunning == false){ - if(getyPos() <= 48f){ - } - else{ - setIsRunning(true); - tdown.start(); - } - } - } - else if(i > 0.6 && i <= 0.8){ - if(isRunning == false){ - if(getxPos() <= 48f){ - } - else{ - setIsRunning(true); - tleft.start(); - } - } - } - else{ - } - } - - - private void setIsRunning(boolean n){ - isRunning = n; - } - private boolean getIsRunning(){ - return isRunning; - } - - public int getTimerRuns(){ - return timerRuns; - } - - public void setTimerRuns(int n){ - timerRuns = n; + 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..95b40e6 100644 --- a/core/src/com/dungeoncrawler/model/entities/Arrow.java +++ b/core/src/com/dungeoncrawler/model/entities/Arrow.java @@ -6,14 +6,9 @@ package com.dungeoncrawler.model.entities; import com.dungeoncrawler.model.Entity; -import com.badlogic.gdx.utils.Timer; public class Arrow extends Entity{ - Timer toben; - Timer tunten; - Timer trechts; - Timer tlinks; float xStart; float yStart; int direction; @@ -22,61 +17,11 @@ public class Arrow extends Entity{ super(xPos, yPos, lvl); xStart = xPos; yStart = yPos; - Timer toben = new Timer(); - Timer tunten = new Timer(); - Timer trechts = new Timer(); - Timer tlinks = new Timer(); this.direction = direction; this.dmg = 3*lvl; this.id = 2; - toben.scheduleTask(new Timer.Task() { - @Override - public void run() { - setyPos(getyPos() + 3f); - } - },0,0.1f); - toben.stop(); - tunten.scheduleTask(new Timer.Task() { - @Override - public void run() { - setyPos(getyPos() - 3f); - } - },0,0.1f); - tunten.stop(); - trechts.scheduleTask(new Timer.Task() { - @Override - public void run() { - setxPos(getxPos() + 3f); - } - },0,0.1f); - trechts.stop(); - tlinks.scheduleTask(new Timer.Task() { - @Override - public void run() { - setyPos(getxPos() - 3f); - } - },0,0.1f); - tlinks.stop(); - switch(direction){ - case 0: - toben.start(); - break; - case 1: - trechts.start(); - break; - case 2: - tunten.start(); - break; - case 3: - tlinks.start(); - break; - - } - - - } - + } public float getxStart(){ return xStart; @@ -84,4 +29,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 0e8d2b6..a1c7080 100644 --- a/core/src/com/dungeoncrawler/model/entities/Player.java +++ b/core/src/com/dungeoncrawler/model/entities/Player.java @@ -5,11 +5,9 @@ */ package com.dungeoncrawler.model.entities; - import com.dungeoncrawler.model.Entity; import com.dungeoncrawler.model.Inventory; import com.dungeoncrawler.model.Item; -import com.dungeoncrawler.model.ItemContainer; /** * * @author Jan @@ -40,6 +38,7 @@ public class Player extends Entity { public Inventory getInv(){ return inv; } + public void updateItems(){ if(inv.getItem(1) != null){ dmg = standartDmg + inv.getItem(1).getDmg(); @@ -49,6 +48,7 @@ public class Player extends Entity { dmg = standartDmg; maxhp = standartMaxHp; } - } + + } diff --git a/core/src/com/dungeoncrawler/model/entities/Swordsman.java b/core/src/com/dungeoncrawler/model/entities/Swordsman.java index 4eaf03a..3baa52f 100644 --- a/core/src/com/dungeoncrawler/model/entities/Swordsman.java +++ b/core/src/com/dungeoncrawler/model/entities/Swordsman.java @@ -1,151 +1,53 @@ package com.dungeoncrawler.model.entities; -import com.badlogic.gdx.utils.Timer; + import com.dungeoncrawler.model.Entity; public class Swordsman extends Entity { - Timer tup; - Timer tright; - Timer tdown; - Timer tleft; - int timerRuns; - boolean isRunning; - public Swordsman(float xPos, float yPos, int lvl) { super(xPos, yPos, lvl); 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 - tup = new Timer(); - tright = new Timer(); - tdown = new Timer(); - tleft = new Timer(); - isRunning = false; - timerRuns = 0; - facing = 2; - - tup.scheduleTask(new Timer.Task() { - @Override - public void run() { - setFacing(0); - setyPos(getyPos() + 1f); - setTimerRuns(getTimerRuns() + 1); - if(getTimerRuns() >= 48){ - setTimerRuns(0); - setIsRunning(false); - tup.stop(); - } - } - },0,0.025f); - tup.stop(); - tright.scheduleTask(new Timer.Task() { - @Override - public void run() { - setFacing(1); - setxPos(getxPos() + 1f); - setTimerRuns(getTimerRuns() + 1); - if(getTimerRuns() >= 48){ - setTimerRuns(0); - setIsRunning(false); - tright.stop(); - } - } - },0,0.025f); - tright.stop(); - tdown.scheduleTask(new Timer.Task() { - @Override - public void run() { - setFacing(2); - setyPos(getyPos() - 1f); - setTimerRuns(getTimerRuns() + 1); - if(getTimerRuns() >= 48){ - setTimerRuns(0); - setIsRunning(false); - tdown.stop(); - } - } - },0,0.025f); - tdown.stop(); - tleft.scheduleTask(new Timer.Task() { - @Override - public void run() { - setFacing(3); - setxPos(getxPos() - 1f); - setTimerRuns(getTimerRuns() + 1); - - if(getTimerRuns() >= 48){ - setTimerRuns(0); - setIsRunning(false); - tleft.stop(); - } - } - },0,0.025f); - tleft.stop(); + // TODO: Sinnvolle Werte finden + direction = 2; } @Override - public void randomMove(int x, int y) { - double i = Math.random(); - if(i <= 0.25){ - if(isRunning == false){ - if(yPos >= (x-1) * 48f){ - } - else{ - setIsRunning(true); - tup.start(); - } - } - } - else if(i > 0.25 && i <= 0.5){ - if(isRunning == false){ - if(xPos >= (y-1) * 48f){ - } - else{ - setIsRunning(true); - tright.start(); - } - } + 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(i > 0.5 && i <= 0.75){ - if(isRunning == false){ - if(yPos <= 48f){ - } - else{ - setIsRunning(true); - tdown.start(); - } - } + else if(deltaX < 0 && deltaY > 0){ + alpha = Math.PI - alpha; } - else if(i > 0.75 && i <= 1){ - if(isRunning == false){ - if(xPos <= 48f){ - } - else{ - setIsRunning(true); - tleft.start(); - } - } + else if(deltaX > 0 && deltaY < 0){ + alpha = 2*Math.PI - alpha; } - } - - private void setIsRunning(boolean n){ - isRunning = n; - } - private boolean getIsRunning(){ - return isRunning; - } - - public int getTimerRuns(){ - return timerRuns; - } - - public void setTimerRuns(int n){ - timerRuns = n; + } + + movementX = (int) (3 * Math.cos(alpha)); + movementY = (int) (3 * Math.sin(alpha)); + + xPos += movementX; + yPos += movementY; } diff --git a/core/src/com/dungeoncrawler/view/GameScreen.java b/core/src/com/dungeoncrawler/view/GameScreen.java index 11c5f4b..801a5ee 100644 --- a/core/src/com/dungeoncrawler/view/GameScreen.java +++ b/core/src/com/dungeoncrawler/view/GameScreen.java @@ -28,7 +28,7 @@ public class GameScreen { //ENTITIES Texture[] entityTextures; - Sprite[] entitySprites; + public Sprite[] entitySprites; TextureRegion[][] archerRegions; Texture archerTexture; TextureRegion[][] swordsmanRegions; @@ -43,8 +43,8 @@ public class GameScreen { TiledMapRenderer tmr; TiledMap tm; OrthographicCamera camera; - ArrayList objects; - ArrayList mapItems; + public ArrayList objects; + public ArrayList mapItems; Timer animations; Timer animatePlayer; @@ -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);