diff --git a/core/src/com/trs/main/Dialogue.java b/core/src/com/trs/main/Dialogue.java index 4d00fae..a96e55f 100644 --- a/core/src/com/trs/main/Dialogue.java +++ b/core/src/com/trs/main/Dialogue.java @@ -1,6 +1,6 @@ package com.trs.main; public class Dialogue { - String question; - String[] ans; + public String question; + public String[] ans; } diff --git a/core/src/com/trs/main/FightObject.java b/core/src/com/trs/main/FightObject.java deleted file mode 100644 index 49c5ba2..0000000 --- a/core/src/com/trs/main/FightObject.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.trs.main; - -import com.badlogic.gdx.math.Vector2; - -public abstract class FightObject { - protected AnimatedSprite sprite; - protected Stats stats; - protected int id; - 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; - 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 attack(FightObject o) { - if(o != null) { - o.stats.setHp(o.stats.getHp() - stats.getAtk()); - } - } - - public void startAction() { - state = 1; - moves = maxMoves; - } -} diff --git a/core/src/com/trs/main/Hostile.java b/core/src/com/trs/main/Hostile.java deleted file mode 100644 index d5249bf..0000000 --- a/core/src/com/trs/main/Hostile.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.trs.main; - -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.Batch; -import com.badlogic.gdx.math.Circle; -import com.badlogic.gdx.math.Intersector; -import com.badlogic.gdx.math.Rectangle; -import com.badlogic.gdx.math.Vector2; -import com.badlogic.gdx.scenes.scene2d.Actor; - -public class Hostile extends Actor { - - int id; - Stats stats; - AnimatedSprite sprite; - 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, 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); - - collisionRect = new Rectangle(xPos + 16, yPos, 32, 16); - attackCircle = new Circle(xPos + 16, yPos, 100f); - attentionCircle = new Circle(xPos + 16, yPos, 300f); - - movementState = 0; - - setX(xPos); - setY(yPos); - } - - @Override - public void act(float deltatime) { - sprite.updateAnimation(deltatime); - - if(movementState == 0) { - for(Actor a : getStage().getActors()) { - if(a instanceof Player) { - if(Intersector.overlaps(attentionCircle, ((Player) a).collisionRect)) { - movementState = 1; - } - } - } - } - - if(movementState == 1) { - for(Actor a : getStage().getActors()) { - if(a instanceof Player) { - if(Intersector.overlaps(attackCircle, ((Player) a).collisionRect)) { - movementState = 2; - } - if(!Intersector.overlaps(attentionCircle, ((Player) a).collisionRect)) { - movementState = 0; - } - - - Vector2 POI = new Vector2(a.getX(), a.getY()); - float speed = 2; - - Vector2 movement = new Vector2(speed,0); - movement.setAngleRad(StaticMath.calculateAngle(getX(), getY(), POI.x, POI.y)); - int facing; - if(movement.angleDeg() < 135 && movement.angleDeg() >= 45) { - facing = 0; - } - else if(movement.angleDeg() >= 135 && movement.angleDeg() < 225) { - facing = 1; - } - else if(movement.angleDeg() >= 225 && movement.angleDeg() < 315) { - facing = 2; - } - else { - facing = 3; - } - - if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y) < 1f) { - movement.x = 0; - movement.y = 0; - } - - setX(getX() + movement.x); - setY(getY() + movement.y); - - int animationRow = 0; - if(movement.x != 0 || movement.y != 0) { - animationRow = 8; - } - - sprite.setRow(animationRow + facing); - } - } - } - - if(movementState == 2) { - Main.gamestate = 2; - //System.out.println("Kämpfe mit mir du Spast!"); - } - } - - @Override - public void draw(Batch batch, float deltatime) { - sprite.draw(batch); - } - - @Override - protected void positionChanged() { - sprite.setSpritePosition((int)getX(), (int)getY()); - collisionRect = new Rectangle(getX() + 16, getY(), 32, 16); - attackCircle = new Circle(getX() + 16, getY(), 100f); - attentionCircle = new Circle(getX() + 16, getY(), 300f); - super.positionChanged(); //To change body of generated methods, choose Tools | Templates. - } -} diff --git a/core/src/com/trs/main/InformationQuest.java b/core/src/com/trs/main/InformationQuest.java index 1135403..2bbbf55 100644 --- a/core/src/com/trs/main/InformationQuest.java +++ b/core/src/com/trs/main/InformationQuest.java @@ -5,6 +5,7 @@ */ package com.trs.main; +import com.trs.main.worldobjects.MovingNpc; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.utils.Array; @@ -26,7 +27,7 @@ public class InformationQuest extends Quest{ boolean rightOrderRequired; - InformationQuest(int questId, String questText, int[] informationNpcId, int[] informationNpcMapId, boolean rightOrderRequired){ + public InformationQuest(int questId, String questText, int[] informationNpcId, int[] informationNpcMapId, boolean rightOrderRequired){ this.questId = questId; @@ -43,7 +44,7 @@ public class InformationQuest extends Quest{ } - void talk(MovingNpc a){ + public void talk(MovingNpc a){ for(int i = 0; i < talked.length; i++){ if(informationNpcId[i] == a.id && informationNpcMapId[i] == a.mapId){ talked[i] = true; @@ -53,7 +54,7 @@ public class InformationQuest extends Quest{ } - boolean hasSpecialDialogue(int NpcId, int mapId){ + public boolean hasSpecialDialogue(int NpcId, int mapId){ if(!finished){ if(!rightOrderRequired){ for(int i = 0; i < talked.length; i++){ @@ -79,12 +80,12 @@ public class InformationQuest extends Quest{ else return false; } - String getDialoguePath(int NpcId, int mapId){ + public String getDialoguePath(int NpcId, int mapId){ return "quests/informationQuests/"+questId+"/dialogues/map"+mapId+"/npc"+NpcId+"/dialogue.txt"; } @Override - void updateQuest() { + public void updateQuest() { if(!finished){ finished = true; for(int i = 0; i < talked.length; i++){ @@ -106,7 +107,7 @@ public class InformationQuest extends Quest{ } @Override - boolean finished() { + public boolean finished() { return finished; } diff --git a/core/src/com/trs/main/MapContainer.java b/core/src/com/trs/main/MapContainer.java index 6d6c880..18170c6 100644 --- a/core/src/com/trs/main/MapContainer.java +++ b/core/src/com/trs/main/MapContainer.java @@ -1,5 +1,14 @@ package com.trs.main; +import com.trs.main.worldobjects.MapCollisionObject; +import com.trs.main.worldobjects.Player; +import com.trs.main.worldobjects.Hostile; +import com.trs.main.worldobjects.InteractionObject; +import com.trs.main.worldobjects.MovingNpc; +import com.trs.main.fightscreen.Enemy; +import com.trs.main.fightscreen.FightPlayer; +import com.trs.main.fightscreen.FightObject; +import com.trs.main.fightscreen.FightScreen; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import java.util.ArrayList; @@ -151,8 +160,8 @@ public class MapContainer { } } - p.movementX = 0; - p.movementY = 0; + p.setMovementX(0); + p.setMovementY(0); stage.addActor(p); @@ -169,7 +178,7 @@ public class MapContainer { ArrayList mapRectsTemp = new ArrayList<>(); for(Actor a : stage.getActors()){ if(a instanceof MapCollisionObject){ - mapRectsTemp.add(((MapCollisionObject)a).r); + mapRectsTemp.add(((MapCollisionObject)a).getR()); } } Rectangle[] rects = new Rectangle[mapRectsTemp.size()]; @@ -180,13 +189,13 @@ public class MapContainer { // CREATING FightObject Array // Temporarily only Player ArrayList tempObjects = new ArrayList<>(); - tempObjects.add(new FightPlayer(getPlayer().getX(),getPlayer().getY(),getPlayer().playerSprite, getPlayer().stats, 0)); + tempObjects.add(new FightPlayer(getPlayer().getX(),getPlayer().getY(), getPlayer().getPlayerSprite(), getPlayer().getStats(), 0)); for(Actor a : stage.getActors()) { 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, ((Hostile) a).isMelee); + if(((Hostile) a).getMovementState() > 0) { + ((Hostile) a).setMovementState(2); + Enemy e = new Enemy(a.getX(), a.getY(), ((Hostile) a).getSprite(), ((Hostile) a).getStats(), ((Hostile) a).getId(), ((Hostile) a).isIsMelee()); tempObjects.add(e); } } @@ -202,7 +211,7 @@ public class MapContainer { else if(Main.gamestate == 2){ Main.gamestate = 0; fs.nuke(); - fs.state = 3; + fs.setState(3); } } @@ -218,7 +227,7 @@ public class MapContainer { } for(Actor a : stage.getActors()) { if(a instanceof Player) { - Rectangle rect = ((Player) a).collisionRect; + Rectangle rect = ((Player) a).getCollisionRect(); for(Door d : doors) { if(Intersector.overlaps(rect, d.rect)) { @@ -239,7 +248,7 @@ public class MapContainer { ArrayList mapRectsTemp = new ArrayList<>(); for(Actor a : stage.getActors()){ if(a instanceof MapCollisionObject){ - mapRectsTemp.add(((MapCollisionObject)a).r); + mapRectsTemp.add(((MapCollisionObject)a).getR()); } } Rectangle[] rects = new Rectangle[mapRectsTemp.size()]; @@ -250,11 +259,11 @@ public class MapContainer { // CREATING FightObject Array // Temporarily only Player ArrayList tempObjects = new ArrayList<>(); - tempObjects.add(new FightPlayer(getPlayer().getX(),getPlayer().getY(),getPlayer().playerSprite, getPlayer().stats, 0)); + tempObjects.add(new FightPlayer(getPlayer().getX(),getPlayer().getY(), getPlayer().getPlayerSprite(), getPlayer().getStats(), 0)); 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, ((Hostile) a).isMelee); + Enemy e = new Enemy(a.getX(), a.getY(), ((Hostile) a).getSprite(), ((Hostile) a).getStats(), ((Hostile) a).getId(), ((Hostile) a).isIsMelee()); tempObjects.add(e); } } @@ -267,25 +276,25 @@ public class MapContainer { fs = new FightScreen(stage.getBatch(), fightObjects, rects, getPlayer().getX()+32, getPlayer().getY()+32); } - if(fs.state == 3){ - for(FightObject object : fs.objects){ + if(fs.getState() == 3){ + for(FightObject object : fs.getObjects()){ if(object instanceof FightPlayer){ - getPlayer().setX(object.x); - getPlayer().setY(object.y); - getPlayer().stats = object.stats; + getPlayer().setX(object.getX()); + getPlayer().setY(object.getY()); + getPlayer().setStats(object.getStats()); } else{ for(int i = stage.getActors().size-1; i >= 0; i--){ if(stage.getActors().get(i) instanceof Hostile){ - if(((Hostile)stage.getActors().get(i)).id == object.id){ - if(object.stats.getHp() <= 0){ + if(((Hostile)stage.getActors().get(i)).getId() == object.getId()){ + if(object.getStats().getHp() <= 0){ stage.getActors().removeIndex(i); } else{ - stage.getActors().get(i).setPosition(object.x, object.y); - ((Hostile)stage.getActors().get(i)).stats = object.stats; + stage.getActors().get(i).setPosition(object.getX(), object.getY()); + ((Hostile)stage.getActors().get(i)).setStats(object.getStats()); } } } diff --git a/core/src/com/trs/main/Player.java b/core/src/com/trs/main/Player.java deleted file mode 100644 index 5b51c14..0000000 --- a/core/src/com/trs/main/Player.java +++ /dev/null @@ -1,263 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package com.trs.main; - -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.Input; -import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.Batch; -import com.badlogic.gdx.math.Intersector; -import com.badlogic.gdx.math.Rectangle; -import com.badlogic.gdx.scenes.scene2d.Actor; -import com.badlogic.gdx.scenes.scene2d.Group; -import com.badlogic.gdx.utils.Array; -import java.util.ArrayList; -/** - * - * - * @author Jan - */ -public class Player extends Actor{ - - public static final float SQRT2 = 1.414f; - - Texture t; - AnimatedSprite playerSprite; - float movementX = 0; - float movementY = 0; - float speed = 3f; - float velocity = 0.5f; - // 0: up, 1: left, 2: down, 3: right - int facing = 0; - - ArrayList quests; - - Rectangle collisionRect; - - Group questGroup; - - Stats stats; - - public Player(int xPos, int yPos){ - setName("player"); - t = new Texture(Gdx.files.internal("textureData/sprites/player.png")); - playerSprite = new AnimatedSprite(t, 64, 64, true); - playerSprite.setRow(0); - collisionRect = new Rectangle(xPos + 16, yPos, 32, 16); - setBounds(xPos, yPos, playerSprite.getSprite().getWidth(), playerSprite.getSprite().getHeight()); - quests = new ArrayList<>(); - - stats = new Stats(); - - //TEST QUESTS - int[] n = {1, 1}; - int[] m = {1, 0}; - quests.add(new InformationQuest(0, "Sprich mit Folgenden NPCs: (Id, mapId, schonGereded?) !Reihenfolge wichtig!", m, n, true)); - quests.add(new InformationQuest(1, "jajajaj nicenicenice", m, n, false)); - } - - @Override - protected void positionChanged() { - playerSprite.setSpritePosition((int)getX(), (int)getY()); - collisionRect = new Rectangle(getX() + 16, getY(), 32, 16); - super.positionChanged(); //To change body of generated methods, choose Tools | Templates. - } - - - - @Override - public void act(float delta) { - - // TEST QUESTS - for(Quest quest : quests){ - //quest.print(); - //System.out.println(); - } - //System.out.println(); - - // QUEST HANDLING - for(Quest quest : quests){ - quest.updateQuest(); - } - - // PLAYER ACTING - if(Main.gamestate == 0) { - if(Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT)){ - speed = 9; - } - else speed = 3; - if(Gdx.input.isKeyPressed(Input.Keys.D)){ - movementX = speed; - facing = 3; - } - if(Gdx.input.isKeyPressed(Input.Keys.A)){ - movementX = -speed; - facing = 1; - } - if(Gdx.input.isKeyPressed(Input.Keys.W)){ - movementY = speed; - facing = 0; - } - if(Gdx.input.isKeyPressed(Input.Keys.S)){ - movementY = -speed; - facing = 2; - } - - if(Gdx.input.isKeyPressed(Input.Keys.NUM_1)){ - movementY = -8; - } - if(Gdx.input.isKeyJustPressed(Input.Keys.NUM_2)){ - Main.gamestate = 9; - } - if(Gdx.input.isKeyJustPressed(Input.Keys.E)) { - Actor a = collidingActor(); - if(a != null) { - if(a instanceof MovingNpc){ - Main.gamestate = 1; - boolean dialogueStarted = false; - for(Quest quest : quests){ - if(quest instanceof InformationQuest){ - if(((InformationQuest)quest).hasSpecialDialogue(((MovingNpc) a).id, ((MovingNpc) a).mapId)){ - ((MovingNpc) a).startSpecialDialogue(((InformationQuest)quest).getDialoguePath(((MovingNpc) a).id, ((MovingNpc) a).mapId), getX()+32, getY()+32); - ((InformationQuest) quest).talk((MovingNpc)a); - dialogueStarted = true; - break; - } - } - } - if(!dialogueStarted){ - ((MovingNpc)a).startDialogue(getX()+32, getY()+32); - } - - movementX = 0; - movementY = 0; - } - else if(a instanceof InteractionObject) { - Main.gamestate = 1; - ((InteractionObject)a).startDialogue(getX()+32, getY()+32); - movementX = 0; - movementY = 0; - } - } - } - } - else if(Main.gamestate == 1) { - // Input handled by invoked textbox - } - - // MOVEMENT HANDLING - if(movementX == 0 && movementY == 0){ - - } - else if(movementX == 0 && movementY != 0){ - setY(getY()+movementY); - if(collidingWithMapCollisionObject()){ - setY(getY()-movementY); - } - } - else if(movementY == 0 && movementX != 0){ - setX(getX()+movementX); - if(collidingWithMapCollisionObject()){ - setX(getX()-movementX); - } - } - else if(movementX != 0 && movementY != 0){ - setX(getX()+ (movementX / SQRT2)); - if(collidingWithMapCollisionObject()){ - setX(getX() - (movementX / SQRT2)); - } - - setY(getY() + (movementY / SQRT2)); - if(collidingWithMapCollisionObject()){ - setY(getY()- (movementY / SQRT2)); - } - } - velocity(velocity); - - // ANIMATION HANDLING - int animationRow = 0; - if(movementX != 0 || movementY != 0) { - animationRow = 8; - } - playerSprite.setRow(animationRow + facing); - playerSprite.updateAnimation(delta); - - super.act(delta); //To change body of generated methods, choose Tools | Templates. - - - - } - - @Override - public void draw(Batch batch, float parentAlpha) { - playerSprite.draw(batch); - super.draw(batch, parentAlpha); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public boolean remove() { - return super.remove(); //To change body of generated methods, choose Tools | Templates. - } - - public boolean collidingWithMapCollisionObject(){ - for(Actor a : getStage().getActors()){ - if(a instanceof MapCollisionObject){ - Rectangle o = new Rectangle(a.getX(), a.getY(), a.getWidth(), a.getHeight()); - if(Intersector.overlaps(collisionRect, o)){ - return true; - } - } - else if(a instanceof MovingNpc){ - if(Intersector.overlaps(collisionRect, ((MovingNpc)a).collisionRect)){ - return true; - } - } - } - return false; - } - - // Slowing the players movement by velocity - public void velocity(float velocity){ - if(movementX > 0){ - movementX -= velocity; - if(movementX < 0){ - movementX = 0; - } - } - else if(movementX < 0){ - movementX += velocity; - if(movementX > 0){ - movementX = 0; - } - } - - if(movementY > 0){ - movementY -= velocity; - if(movementY < 0){ - movementY = 0; - } - } - else if(movementY < 0){ - movementY += velocity; - if(movementY > 0){ - movementY = 0; - } - } - } - - public Actor collidingActor(){ - for(Actor a : getStage().getActors()){ - if(a instanceof InteractionObject || a instanceof MovingNpc){ - Rectangle p = new Rectangle(getX(), getY(), getWidth(), getHeight()); - Rectangle npc = new Rectangle(a.getX(), a.getY(), a.getWidth(), a.getHeight()); - if(Intersector.overlaps(p, npc)){ - return a; - } - } - } - return null; - } -} diff --git a/core/src/com/trs/main/Quest.java b/core/src/com/trs/main/Quest.java index 7cbb448..ac66f42 100644 --- a/core/src/com/trs/main/Quest.java +++ b/core/src/com/trs/main/Quest.java @@ -20,7 +20,7 @@ public abstract class Quest { - abstract void updateQuest(); - abstract boolean finished(); - abstract void print(); + abstract public void updateQuest(); + public abstract boolean finished(); + public abstract void print(); } diff --git a/core/src/com/trs/main/Enemy.java b/core/src/com/trs/main/fightscreen/Enemy.java similarity index 55% rename from core/src/com/trs/main/Enemy.java rename to core/src/com/trs/main/fightscreen/Enemy.java index 0cfb1db..bb5c688 100644 --- a/core/src/com/trs/main/Enemy.java +++ b/core/src/com/trs/main/fightscreen/Enemy.java @@ -1,6 +1,9 @@ -package com.trs.main; +package com.trs.main.fightscreen; import com.badlogic.gdx.math.Vector2; +import com.trs.main.worldobjects.AnimatedSprite; +import com.trs.main.StaticMath; +import com.trs.main.Stats; public class Enemy extends FightObject{ @@ -13,26 +16,26 @@ public class Enemy extends FightObject{ } public void act(FightPlayer player){ - if(POI == null && !move){ - double distance = StaticMath.calculateDistance(x, y, player.x, player.y); - System.out.println("PLayer pos " + player.x + " " + player.y); - System.out.println("Meine pos " + x + " " + y); + if(getPOI() == null && !move){ + double distance = StaticMath.calculateDistance(getX(), getY(), player.getX(), player.getY()); + System.out.println("PLayer pos " + player.getX() + " " + player.getY()); + System.out.println("Meine pos " + getX() + " " + getY()); System.out.println("ich bin " + isMelee + "mit Distanz " + distance); if(isMelee) { if(distance <= 32f) { System.out.println("Jetzt stirbst du *kawumm*"); attack(player); - moves--; - state = 2; + setMoves(getMoves() - 1); + setState(2); } else { - float tempX = x; - float tempY = y; + float tempX = getX(); + float tempY = getY(); - float deltaX = player.x - x; - float deltaY = player.y - y; + float deltaX = player.getX() - getX(); + float deltaY = player.getY() - getY(); if(Math.abs(deltaX) >= Math.abs(deltaY)) { tempX += (deltaX / Math.abs(deltaX)) * 32; @@ -41,8 +44,8 @@ public class Enemy extends FightObject{ tempY += (deltaY / Math.abs(deltaY)) * 32; } - POI = new Vector2(tempX, tempY); - moves--; + setPOI(new Vector2(tempX, tempY)); + setMoves(getMoves() - 1); move = true; } } @@ -50,16 +53,16 @@ public class Enemy extends FightObject{ if(distance >= 96f && distance <= 150f) { System.out.println("Wilhelm Tell is ein Scheiß gegen mich *surr*"); attack(player); - moves--; - state = 2; + setMoves(getMoves() - 1); + setState(2); } else if(distance < 96f){ - float tempX = x; - float tempY = y; + float tempX = getX(); + float tempY = getY(); - float deltaX = player.x - x; - float deltaY = player.y - y; + float deltaX = player.getX() - getX(); + float deltaY = player.getY() - getY(); if(Math.abs(deltaX) >= Math.abs(deltaY)) { tempX -= (deltaX / Math.abs(deltaX)) * 32; @@ -68,16 +71,16 @@ public class Enemy extends FightObject{ tempY -= (deltaY / Math.abs(deltaY)) * 32; } - POI = new Vector2(tempX, tempY); - moves--; + setPOI(new Vector2(tempX, tempY)); + setMoves(getMoves() - 1); move = true; } else { - float tempX = x; - float tempY = y; + float tempX = getX(); + float tempY = getY(); - float deltaX = player.x - x; - float deltaY = player.y - y; + float deltaX = player.getX() - getX(); + float deltaY = player.getY() - getY(); if(Math.abs(deltaX) >= Math.abs(deltaY)) { tempX += (deltaX / Math.abs(deltaX)) * 32; @@ -86,14 +89,14 @@ public class Enemy extends FightObject{ tempY += (deltaY / Math.abs(deltaY)) * 32; } - POI = new Vector2(tempX, tempY); - moves--; + setPOI(new Vector2(tempX, tempY)); + setMoves(getMoves() - 1); move = true; } } } - else if(move && POI == null){ - state = 2; + else if(move && getPOI() == null){ + setState(2); move = false; } } diff --git a/core/src/com/trs/main/FightDialogue.java b/core/src/com/trs/main/fightscreen/FightDialogue.java similarity index 98% rename from core/src/com/trs/main/FightDialogue.java rename to core/src/com/trs/main/fightscreen/FightDialogue.java index 2851997..edf23f3 100644 --- a/core/src/com/trs/main/FightDialogue.java +++ b/core/src/com/trs/main/fightscreen/FightDialogue.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package com.trs.main; +package com.trs.main.fightscreen; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; @@ -14,6 +14,7 @@ import com.badlogic.gdx.graphics.g2d.GlyphLayout; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.Rectangle; +import com.trs.main.Main; /** * diff --git a/core/src/com/trs/main/fightscreen/FightObject.java b/core/src/com/trs/main/fightscreen/FightObject.java new file mode 100644 index 0000000..1c2c888 --- /dev/null +++ b/core/src/com/trs/main/fightscreen/FightObject.java @@ -0,0 +1,149 @@ +package com.trs.main.fightscreen; + +import com.badlogic.gdx.math.Vector2; +import com.trs.main.worldobjects.AnimatedSprite; +import com.trs.main.Stats; + +public abstract class FightObject { + private AnimatedSprite sprite; + private Stats stats; + private int id; + private float x; + private float y; + private Vector2 POI; + private int moves; + private int maxMoves; + + // 0: waiting 1: doing action 2: finished action + private 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; + maxMoves = 2; + moves = maxMoves; + } + + void setX(float x) { + this.x = x; + this.getSprite().setSpritePosition((int)this.x, (int)this.getY()); + } + + void setY(float y) { + this.y = y; + this.getSprite().setSpritePosition((int)this.getX(), (int)this.y); + } + + void attack(FightObject o) { + if(o != null) { + o.getStats().setHp(o.getStats().getHp() - getStats().getAtk()); + } + } + + public float getX(){ + return x; + } + public float getY(){ + return y; + } + + public Stats getStats(){ + return stats; + } + public void setStats(Stats stats){ + this.stats = stats; + } + + public void startAction() { + setState(1); + setMoves(getMaxMoves()); + } + + /** + * @return the sprite + */ + public AnimatedSprite getSprite() { + return sprite; + } + + /** + * @param sprite the sprite to set + */ + public void setSprite(AnimatedSprite sprite) { + this.sprite = sprite; + } + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(int id) { + this.id = id; + } + + /** + * @return the POI + */ + public Vector2 getPOI() { + return POI; + } + + /** + * @param POI the POI to set + */ + public void setPOI(Vector2 POI) { + this.POI = POI; + } + + /** + * @return the moves + */ + public int getMoves() { + return moves; + } + + /** + * @param moves the moves to set + */ + public void setMoves(int moves) { + this.moves = moves; + } + + /** + * @return the maxMoves + */ + public int getMaxMoves() { + return maxMoves; + } + + /** + * @param maxMoves the maxMoves to set + */ + public void setMaxMoves(int maxMoves) { + this.maxMoves = maxMoves; + } + + /** + * @return the state + */ + public int getState() { + return state; + } + + /** + * @param state the state to set + */ + public void setState(int state) { + this.state = state; + } + +} diff --git a/core/src/com/trs/main/FightPlayer.java b/core/src/com/trs/main/fightscreen/FightPlayer.java similarity index 63% rename from core/src/com/trs/main/FightPlayer.java rename to core/src/com/trs/main/fightscreen/FightPlayer.java index 5b116a0..2d410ff 100644 --- a/core/src/com/trs/main/FightPlayer.java +++ b/core/src/com/trs/main/fightscreen/FightPlayer.java @@ -1,4 +1,7 @@ -package com.trs.main; +package com.trs.main.fightscreen; + +import com.trs.main.worldobjects.AnimatedSprite; +import com.trs.main.Stats; public class FightPlayer extends FightObject{ diff --git a/core/src/com/trs/main/FightScreen.java b/core/src/com/trs/main/fightscreen/FightScreen.java similarity index 58% rename from core/src/com/trs/main/FightScreen.java rename to core/src/com/trs/main/fightscreen/FightScreen.java index 073242e..4b7862d 100644 --- a/core/src/com/trs/main/FightScreen.java +++ b/core/src/com/trs/main/fightscreen/FightScreen.java @@ -1,4 +1,4 @@ -package com.trs.main; +package com.trs.main.fightscreen; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; @@ -8,6 +8,8 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.Intersector; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; +import com.trs.main.Main; +import com.trs.main.StaticMath; public class FightScreen { @@ -37,34 +39,34 @@ public class FightScreen { gridPos.x = (float)(Math.ceil((double)(camX-Main.CAMERA_WIDTH/2)/32.0) * 32.0) + 64; gridPos.y = (float)(Math.ceil((double)(camY-Main.CAMERA_HEIGHT/2)/32.0) * 32.0) + 32; - - - // SORTING OBJECTS BY INITIATIVE STAT - - for(int j = 0; j < objects.length-1; j++){ - for(int i = objects.length-1; i >= 0; i--){ - if(i > 0 && objects[i].stats.getInit() > objects[i-1].stats.getInit()){ - FightObject temp = objects[i-1]; - objects[i-1] = objects[i]; - objects[i] = temp; - } + + + // SORTING OBJECTS BY INITIATIVE STAT + + for(int j = 0; j < objects.length-1; j++){ + for(int i = objects.length-1; i >= 0; i--){ + if(i > 0 && objects[i].getStats().getInit() > objects[i-1].getStats().getInit()){ + FightObject temp = objects[i-1]; + objects[i-1] = objects[i]; + objects[i] = temp; } } - objects[0].startAction(); + } + objects[0].startAction(); } public void act(float deltatime) { - System.out.println(getPlayer().state); + System.out.println(getPlayer().getState()); if(state == 0){ boolean finished = true; for(FightObject object : objects){ - if(object.x % 32 != 0 || object.y % 32 != 0){ - object.POI = new Vector2((int)(Math.ceil((double)(object.x)/32.0) * 32.0) - 16, (int)(Math.ceil((double)(object.y)/32.0) * 32.0)); + if(object.getX() % 32 != 0 || object.getY() % 32 != 0){ + object.setPOI(getNearestGridTile(object.getX(), object.getY())); } } gotoPOI(); for(FightObject object : objects){ - if(object.POI != null){ + if(object.getPOI() != null){ finished = false; break; } @@ -75,35 +77,35 @@ public class FightScreen { } else if(state == 1){ for(int i = 0; i < objects.length; i++){ - if(objects[i].state == 1){ + if(objects[i].getState() == 1){ if(objects[i] instanceof FightPlayer){ if(Gdx.input.isKeyJustPressed(Input.Keys.W)){ - if(objects[i].POI == null){ - objects[i].POI = new Vector2(objects[i].x, objects[i].y + 32); + if(objects[i].getPOI() == null){ + objects[i].setPOI(new Vector2(objects[i].getX(), objects[i].getY() + 32)); System.out.println("W"); } } if(Gdx.input.isKeyJustPressed(Input.Keys.A)){ - if(objects[i].POI == null){ - objects[i].POI = new Vector2(objects[i].x-32, objects[i].y); + if(objects[i].getPOI() == null){ + objects[i].setPOI(new Vector2(objects[i].getX() - 32, objects[i].getY())); System.out.println("A"); } } if(Gdx.input.isKeyJustPressed(Input.Keys.S)){ - if(objects[i].POI == null){ - objects[i].POI = new Vector2(objects[i].x, objects[i].y - 32); + if(objects[i].getPOI() == null){ + objects[i].setPOI(new Vector2(objects[i].getX(), objects[i].getY() - 32)); System.out.println("S"); } } if(Gdx.input.isKeyJustPressed(Input.Keys.D)){ - if(objects[i].POI == null){ - objects[i].POI = new Vector2(objects[i].x + 32, objects[i].y); + if(objects[i].getPOI() == null){ + objects[i].setPOI(new Vector2(objects[i].getX() + 32, objects[i].getY())); System.out.println("D"); } } if(Gdx.input.isKeyJustPressed(Input.Keys.F)){ System.out.println("F"); - objects[i].state = 2; + objects[i].setState(2); } } else if(objects[i] instanceof Enemy){ @@ -115,8 +117,8 @@ public class FightScreen { } } } - else if(objects[i].state == 2){ - objects[i].state = 0; + else if(objects[i].getState() == 2){ + objects[i].setState(0); if(i == objects.length-1){ objects[0].startAction(); } @@ -134,7 +136,7 @@ public class FightScreen { } gotoPOI(); for(FightObject object : objects) { - object.sprite.updateAnimation(deltatime); + object.getSprite().updateAnimation(deltatime); } } @@ -162,7 +164,7 @@ public class FightScreen { batch.begin(); for(FightObject object : objects) { - object.sprite.draw(batch); + object.getSprite().draw(batch); } batch.end(); @@ -180,7 +182,7 @@ public class FightScreen { public void nuke() { for(FightObject object : objects) { if(object instanceof Enemy) { - object.stats.setHp(-5); + object.getStats().setHp(-5); } } } @@ -195,19 +197,32 @@ public class FightScreen { return null; } + public void setState(int state){ + this.state = state; + } + public int getState(){ + return state; + } + + public FightObject[] getObjects(){ + return objects; + } + + + public void gotoPOI(){ for(FightObject object : objects){ - if(object.POI != null){ + if(object.getPOI() != null){ //object.POI = new Vector2((int)(Math.ceil((double)(object.x)/32.0) * 32.0) - 16, (int)(Math.ceil((double)(object.y)/32.0) * 32.0)); float speed = 3f; - if(StaticMath.calculateDistance(object.x, object.y, object.POI.x, object.POI.y) < 3f && StaticMath.calculateDistance(object.x, object.y, object.POI.x, object.POI.y) != 0) { - speed = Math.abs(Vector2.dst(object.x, object.y, object.POI.x, object.POI.y)); + if(StaticMath.calculateDistance(object.getX(), object.getY(), object.getPOI().x, object.getPOI().y) < 3f && StaticMath.calculateDistance(object.getX(), object.getY(), object.getPOI().x, object.getPOI().y) != 0) { + speed = Math.abs(Vector2.dst(object.getX(), object.getY(), object.getPOI().x, object.getPOI().y)); } Vector2 movement = new Vector2(speed,0); - movement.setAngleRad(StaticMath.calculateAngle(object.x, object.y, object.POI.x, object.POI.y)); + movement.setAngleRad(StaticMath.calculateAngle(object.getX(), object.getY(), object.getPOI().x, object.getPOI().y)); int facing; if(movement.angleDeg() < 135 && movement.angleDeg() >= 45) { facing = 0; @@ -222,24 +237,82 @@ public class FightScreen { facing = 3; } - if((int)object.x == (int)object.POI.x && (int)object.y == (int)object.POI.y) { + if((int)object.getX() == (int)object.getPOI().x && (int)object.getY() == (int)object.getPOI().y) { movement.x = 0; movement.y = 0; } - object.setX(object.x + movement.x); - object.setY(object.y + movement.y); + object.setX(object.getX() + movement.x); + object.setY(object.getY() + movement.y); int animationRow = 0; if(movement.x != 0 || movement.y != 0) { animationRow = 8; } else{ - object.POI = null; + object.setPOI(null); } - object.sprite.setRow(animationRow + facing); + object.getSprite().setRow(animationRow + facing); } } } - + + public Vector2 getNearestGridTile(float x, float y){ + + //o o o + //o + o + //x o o + if(x <= gridPos.x && y <= gridPos.y){ + return new Vector2(gridPos.x -16, gridPos.y); + } + //o o o + //x + o + //o o o + else if(x < gridPos.x && y > gridPos.y && y < gridPos.y + gridHeight*32){ + return new Vector2(gridPos.x -16, (int) (Math.ceil((double) (y) / 32.0) * 32.0)); + } + //x o o + //o + o + //o o o + else if(x <= gridPos.x && y >= gridPos.y + gridHeight*32){ + return new Vector2(gridPos.x -16, gridPos.y + gridHeight*32); + } + //o x o + //o + o + //o o o + else if(x > gridPos.x && x < gridPos.x + gridWidth*32 && y > gridPos.y + gridHeight*32){ + return new Vector2((int) (Math.ceil((double) (x) / 32.0) * 32.0) -16, gridPos.y + gridHeight*32); + } + //o o x + //o + o + //o o o + else if(x >= gridPos.x + gridWidth*32 && y >= gridPos.y + gridHeight*32){ + return new Vector2(gridPos.x + gridWidth*32 -16, gridPos.y + gridHeight*32); + } + //o o o + //o + x + //o o o + else if(x > gridPos.x + gridWidth*32 && y < gridPos.y + gridHeight*32 && y > gridPos.y){ + return new Vector2(gridPos.x + (gridWidth-1)*32 -16, (int) (Math.ceil((double) (y) / 32.0) * 32.0)); + } + //o o o + //o + o + //o o x + else if(x >= gridPos.x + gridWidth*32 && y <= gridPos.y){ + return new Vector2(gridPos.x + gridWidth*32 -16, gridPos.y); + } + //o o o + //o + o + //o x o + else if(x > gridPos.x && x < gridPos.x + gridWidth*32 && y < gridPos.y){ + return new Vector2((int) (Math.ceil((double) (x) / 32.0) * 32.0) -16, gridPos.y); + } + //o o o + //o x o + //o o o + else{ + return new Vector2((int) (Math.ceil((double) (x) / 32.0) * 32.0) - 16, (int) (Math.ceil((double) (y) / 32.0) * 32.0)); + } + } + } diff --git a/core/src/com/trs/main/view/screens/GameScreen.java b/core/src/com/trs/main/view/screens/GameScreen.java index b699c49..7ef6347 100644 --- a/core/src/com/trs/main/view/screens/GameScreen.java +++ b/core/src/com/trs/main/view/screens/GameScreen.java @@ -8,7 +8,7 @@ package com.trs.main.view.screens; import com.badlogic.gdx.Game; import com.trs.main.Main; import com.trs.main.MapContainer; -import com.trs.main.Player; +import com.trs.main.worldobjects.Player; /** * diff --git a/core/src/com/trs/main/AnimatedSprite.java b/core/src/com/trs/main/worldobjects/AnimatedSprite.java similarity index 98% rename from core/src/com/trs/main/AnimatedSprite.java rename to core/src/com/trs/main/worldobjects/AnimatedSprite.java index 9bd78c8..e786f38 100644 --- a/core/src/com/trs/main/AnimatedSprite.java +++ b/core/src/com/trs/main/worldobjects/AnimatedSprite.java @@ -1,4 +1,4 @@ -package com.trs.main; +package com.trs.main.worldobjects; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Batch; diff --git a/core/src/com/trs/main/worldobjects/Hostile.java b/core/src/com/trs/main/worldobjects/Hostile.java new file mode 100644 index 0000000..7e6aba8 --- /dev/null +++ b/core/src/com/trs/main/worldobjects/Hostile.java @@ -0,0 +1,242 @@ +package com.trs.main.worldobjects; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.math.Circle; +import com.badlogic.gdx.math.Intersector; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.math.Vector2; +import com.badlogic.gdx.scenes.scene2d.Actor; +import com.trs.main.Main; +import com.trs.main.StaticMath; +import com.trs.main.Stats; + +public class Hostile extends Actor { + + private int id; + private Stats stats; + private AnimatedSprite sprite; + private Rectangle collisionRect; + private Circle attackCircle; + private Circle attentionCircle; + private boolean isMelee; + + // 0: normal movement, 1: locked onto Player, 2: attacking + private int movementState; + + 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); + + collisionRect = new Rectangle(xPos + 16, yPos, 32, 16); + attackCircle = new Circle(xPos + 16, yPos, 100f); + attentionCircle = new Circle(xPos + 16, yPos, 300f); + + movementState = 0; + + setX(xPos); + setY(yPos); + } + + @Override + public void act(float deltatime) { + getSprite().updateAnimation(deltatime); + + if(getMovementState() == 0) { + for(Actor a : getStage().getActors()) { + if(a instanceof Player) { + if(Intersector.overlaps(getAttentionCircle(), ((Player) a).getCollisionRect())) { + setMovementState(1); + } + } + } + } + + if(getMovementState() == 1) { + for(Actor a : getStage().getActors()) { + if(a instanceof Player) { + if(Intersector.overlaps(getAttackCircle(), ((Player) a).getCollisionRect())) { + setMovementState(2); + } + if(!Intersector.overlaps(attentionCircle, ((Player) a).getCollisionRect())) { + setMovementState(0); + } + + + Vector2 POI = new Vector2(a.getX(), a.getY()); + float speed = 2; + + Vector2 movement = new Vector2(speed,0); + movement.setAngleRad(StaticMath.calculateAngle(getX(), getY(), POI.x, POI.y)); + int facing; + if(movement.angleDeg() < 135 && movement.angleDeg() >= 45) { + facing = 0; + } + else if(movement.angleDeg() >= 135 && movement.angleDeg() < 225) { + facing = 1; + } + else if(movement.angleDeg() >= 225 && movement.angleDeg() < 315) { + facing = 2; + } + else { + facing = 3; + } + + if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y) < 1f) { + movement.x = 0; + movement.y = 0; + } + + setX(getX() + movement.x); + setY(getY() + movement.y); + + int animationRow = 0; + if(movement.x != 0 || movement.y != 0) { + animationRow = 8; + } + + getSprite().setRow(animationRow + facing); + } + } + } + + if(getMovementState() == 2) { + Main.gamestate = 2; + //System.out.println("Kämpfe mit mir du Spast!"); + } + } + + @Override + public void draw(Batch batch, float deltatime) { + getSprite().draw(batch); + } + + @Override + protected void positionChanged() { + getSprite().setSpritePosition((int)getX(), (int)getY()); + setCollisionRect(new Rectangle(getX() + 16, getY(), 32, 16)); + setAttackCircle(new Circle(getX() + 16, getY(), 100f)); + setAttentionCircle(new Circle(getX() + 16, getY(), 300f)); + super.positionChanged(); //To change body of generated methods, choose Tools | Templates. + } + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(int id) { + this.id = id; + } + + /** + * @return the stats + */ + public Stats getStats() { + return stats; + } + + /** + * @param stats the stats to set + */ + public void setStats(Stats stats) { + this.stats = stats; + } + + /** + * @return the sprite + */ + public AnimatedSprite getSprite() { + return sprite; + } + + /** + * @param sprite the sprite to set + */ + public void setSprite(AnimatedSprite sprite) { + this.sprite = sprite; + } + + /** + * @return the collisionRect + */ + public Rectangle getCollisionRect() { + return collisionRect; + } + + /** + * @param collisionRect the collisionRect to set + */ + public void setCollisionRect(Rectangle collisionRect) { + this.collisionRect = collisionRect; + } + + /** + * @return the attackCircle + */ + public Circle getAttackCircle() { + return attackCircle; + } + + /** + * @param attackCircle the attackCircle to set + */ + public void setAttackCircle(Circle attackCircle) { + this.attackCircle = attackCircle; + } + + /** + * @return the attentionCircle + */ + public Circle getAttentionCircle() { + return attentionCircle; + } + + /** + * @param attentionCircle the attentionCircle to set + */ + public void setAttentionCircle(Circle attentionCircle) { + this.attentionCircle = attentionCircle; + } + + /** + * @return the isMelee + */ + public boolean isIsMelee() { + return isMelee; + } + + /** + * @param isMelee the isMelee to set + */ + public void setIsMelee(boolean isMelee) { + this.isMelee = isMelee; + } + + /** + * @return the movementState + */ + public int getMovementState() { + return movementState; + } + + /** + * @param movementState the movementState to set + */ + public void setMovementState(int movementState) { + this.movementState = movementState; + } + + +} diff --git a/core/src/com/trs/main/InteractionObject.java b/core/src/com/trs/main/worldobjects/InteractionObject.java similarity index 96% rename from core/src/com/trs/main/InteractionObject.java rename to core/src/com/trs/main/worldobjects/InteractionObject.java index f24d3e6..6ad685a 100644 --- a/core/src/com/trs/main/InteractionObject.java +++ b/core/src/com/trs/main/worldobjects/InteractionObject.java @@ -1,4 +1,4 @@ -package com.trs.main; +package com.trs.main.worldobjects; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; @@ -6,6 +6,10 @@ import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.math.Intersector; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.scenes.scene2d.Actor; +import com.trs.main.Dialogue; +import com.trs.main.DialogueParser; +import com.trs.main.Main; +import com.trs.main.Textbox; public class InteractionObject extends Actor{ Textbox t; diff --git a/core/src/com/trs/main/MapCollisionObject.java b/core/src/com/trs/main/worldobjects/MapCollisionObject.java similarity index 77% rename from core/src/com/trs/main/MapCollisionObject.java rename to core/src/com/trs/main/worldobjects/MapCollisionObject.java index a6fbff0..68d3cf5 100644 --- a/core/src/com/trs/main/MapCollisionObject.java +++ b/core/src/com/trs/main/worldobjects/MapCollisionObject.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package com.trs.main; +package com.trs.main.worldobjects; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.math.Rectangle; @@ -15,7 +15,7 @@ import com.badlogic.gdx.scenes.scene2d.Actor; */ public class MapCollisionObject extends Actor{ - Rectangle r; + private Rectangle r; public MapCollisionObject(int x, int y, int width, int height){ setName("mapobject"); @@ -32,5 +32,21 @@ public class MapCollisionObject extends Actor{ public void draw(Batch batch, float parentAlpha) { super.draw(batch, parentAlpha); //To change body of generated methods, choose Tools | Templates. } + + /** + * @return the r + */ + public Rectangle getR() { + return r; + } + + /** + * @param r the r to set + */ + public void setR(Rectangle r) { + this.r = r; + } + + } diff --git a/core/src/com/trs/main/MovingNpc.java b/core/src/com/trs/main/worldobjects/MovingNpc.java similarity index 97% rename from core/src/com/trs/main/MovingNpc.java rename to core/src/com/trs/main/worldobjects/MovingNpc.java index 19cefe8..0b5558d 100644 --- a/core/src/com/trs/main/MovingNpc.java +++ b/core/src/com/trs/main/worldobjects/MovingNpc.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package com.trs.main; +package com.trs.main.worldobjects; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; @@ -12,6 +12,11 @@ import com.badlogic.gdx.math.Intersector; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.scenes.scene2d.Actor; +import com.trs.main.Dialogue; +import com.trs.main.DialogueParser; +import com.trs.main.Main; +import com.trs.main.StaticMath; +import com.trs.main.Textbox; /** * @@ -223,7 +228,7 @@ public class MovingNpc extends Actor{ } } else if(a instanceof Player){ - if(Intersector.overlaps(collisionRect, ((Player)a).collisionRect)){ + if(Intersector.overlaps(collisionRect, ((Player)a).getCollisionRect())){ return true; } } diff --git a/core/src/com/trs/main/worldobjects/Player.java b/core/src/com/trs/main/worldobjects/Player.java new file mode 100644 index 0000000..4d9b8e8 --- /dev/null +++ b/core/src/com/trs/main/worldobjects/Player.java @@ -0,0 +1,437 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.trs.main.worldobjects; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.Input; +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Batch; +import com.badlogic.gdx.math.Intersector; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.scenes.scene2d.Actor; +import com.badlogic.gdx.scenes.scene2d.Group; +import com.badlogic.gdx.utils.Array; +import com.trs.main.InformationQuest; +import com.trs.main.Main; +import com.trs.main.Quest; +import com.trs.main.Stats; +import java.util.ArrayList; +/** + * + * + * @author Jan + */ +public class Player extends Actor{ + + private static float SQRT2 = 1.414f; + + private Texture t; + private AnimatedSprite playerSprite; + private float movementX = 0; + private float movementY = 0; + private float speed = 3f; + private float velocity = 0.5f; + // 0: up, 1: left, 2: down, 3: right + private int facing = 0; + + private ArrayList quests; + + private Rectangle collisionRect; + + private Group questGroup; + + private Stats stats; + + public Player(int xPos, int yPos){ + setName("player"); + t = new Texture(Gdx.files.internal("textureData/sprites/player.png")); + playerSprite = new AnimatedSprite(getT(), 64, 64, true); + playerSprite.setRow(0); + collisionRect = new Rectangle(xPos + 16, yPos, 32, 16); + setBounds(xPos, yPos, playerSprite.getSprite().getWidth(), playerSprite.getSprite().getHeight()); + quests = new ArrayList<>(); + + stats = new Stats(); + + //TEST QUESTS + int[] n = {1, 1}; + int[] m = {1, 0}; + quests.add(new InformationQuest(0, "Sprich mit Folgenden NPCs: (Id, mapId, schonGereded?) !Reihenfolge wichtig!", m, n, true)); + quests.add(new InformationQuest(1, "jajajaj nicenicenice", m, n, false)); + } + + @Override + protected void positionChanged() { + getPlayerSprite().setSpritePosition((int)getX(), (int)getY()); + setCollisionRect(new Rectangle(getX() + 16, getY(), 32, 16)); + super.positionChanged(); //To change body of generated methods, choose Tools | Templates. + } + + + + @Override + public void act(float delta) { + + // TEST QUESTS + for(Quest quest : getQuests()){ + //quest.print(); + //System.out.println(); + } + //System.out.println(); + + // QUEST HANDLING + for(Quest quest : getQuests()){ + quest.updateQuest(); + } + + // PLAYER ACTING + if(Main.gamestate == 0) { + if(Gdx.input.isKeyPressed(Input.Keys.SHIFT_LEFT)){ + setSpeed(9); + } + else setSpeed(3); + if(Gdx.input.isKeyPressed(Input.Keys.D)){ + setMovementX(getSpeed()); + setFacing(3); + } + if(Gdx.input.isKeyPressed(Input.Keys.A)){ + setMovementX(-getSpeed()); + setFacing(1); + } + if(Gdx.input.isKeyPressed(Input.Keys.W)){ + setMovementY(getSpeed()); + setFacing(0); + } + if(Gdx.input.isKeyPressed(Input.Keys.S)){ + setMovementY(-getSpeed()); + setFacing(2); + } + + if(Gdx.input.isKeyPressed(Input.Keys.NUM_1)){ + setMovementY(-8); + } + if(Gdx.input.isKeyJustPressed(Input.Keys.NUM_2)){ + Main.gamestate = 9; + } + if(Gdx.input.isKeyJustPressed(Input.Keys.E)) { + Actor a = collidingActor(); + if(a != null) { + if(a instanceof MovingNpc){ + Main.gamestate = 1; + boolean dialogueStarted = false; + for(Quest quest : getQuests()){ + if(quest instanceof InformationQuest){ + if(((InformationQuest)quest).hasSpecialDialogue(((MovingNpc) a).id, ((MovingNpc) a).mapId)){ + ((MovingNpc) a).startSpecialDialogue(((InformationQuest)quest).getDialoguePath(((MovingNpc) a).id, ((MovingNpc) a).mapId), getX()+32, getY()+32); + ((InformationQuest) quest).talk((MovingNpc)a); + dialogueStarted = true; + break; + } + } + } + if(!dialogueStarted){ + ((MovingNpc)a).startDialogue(getX()+32, getY()+32); + } + + setMovementX(0); + setMovementY(0); + } + else if(a instanceof InteractionObject) { + Main.gamestate = 1; + ((InteractionObject)a).startDialogue(getX()+32, getY()+32); + setMovementX(0); + setMovementY(0); + } + } + } + } + else if(Main.gamestate == 1) { + // Input handled by invoked textbox + } + + // MOVEMENT HANDLING + if(getMovementX() == 0 && getMovementY() == 0){ + + } + else if(getMovementX() == 0 && getMovementY() != 0){ + setY(getY()+getMovementY()); + if(collidingWithMapCollisionObject()){ + setY(getY()-getMovementY()); + } + } + else if(getMovementY() == 0 && getMovementX() != 0){ + setX(getX()+getMovementX()); + if(collidingWithMapCollisionObject()){ + setX(getX()-getMovementX()); + } + } + else if(getMovementX() != 0 && getMovementY() != 0){ + setX(getX()+ (getMovementX() / getSQRT2())); + if(collidingWithMapCollisionObject()){ + setX(getX() - (getMovementX() / getSQRT2())); + } + + setY(getY() + (getMovementY() / getSQRT2())); + if(collidingWithMapCollisionObject()){ + setY(getY()- (getMovementY() / getSQRT2())); + } + } + velocity(getVelocity()); + + // ANIMATION HANDLING + int animationRow = 0; + if(getMovementX() != 0 || getMovementY() != 0) { + animationRow = 8; + } + getPlayerSprite().setRow(animationRow + getFacing()); + getPlayerSprite().updateAnimation(delta); + + super.act(delta); //To change body of generated methods, choose Tools | Templates. + + + + } + + @Override + public void draw(Batch batch, float parentAlpha) { + getPlayerSprite().draw(batch); + super.draw(batch, parentAlpha); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public boolean remove() { + return super.remove(); //To change body of generated methods, choose Tools | Templates. + } + + public boolean collidingWithMapCollisionObject(){ + for(Actor a : getStage().getActors()){ + if(a instanceof MapCollisionObject){ + Rectangle o = new Rectangle(a.getX(), a.getY(), a.getWidth(), a.getHeight()); + if(Intersector.overlaps(getCollisionRect(), o)){ + return true; + } + } + else if(a instanceof MovingNpc){ + if(Intersector.overlaps(getCollisionRect(), ((MovingNpc)a).collisionRect)){ + return true; + } + } + } + return false; + } + + // Slowing the players movement by velocity + public void velocity(float velocity){ + if(getMovementX() > 0){ + setMovementX(getMovementX() - velocity); + if(getMovementX() < 0){ + setMovementX(0); + } + } + else if(getMovementX() < 0){ + setMovementX(getMovementX() + velocity); + if(getMovementX() > 0){ + setMovementX(0); + } + } + + if(getMovementY() > 0){ + setMovementY(getMovementY() - velocity); + if(getMovementY() < 0){ + setMovementY(0); + } + } + else if(getMovementY() < 0){ + setMovementY(getMovementY() + velocity); + if(getMovementY() > 0){ + setMovementY(0); + } + } + } + + public Actor collidingActor(){ + for(Actor a : getStage().getActors()){ + if(a instanceof InteractionObject || a instanceof MovingNpc){ + Rectangle p = new Rectangle(getX(), getY(), getWidth(), getHeight()); + Rectangle npc = new Rectangle(a.getX(), a.getY(), a.getWidth(), a.getHeight()); + if(Intersector.overlaps(p, npc)){ + return a; + } + } + } + return null; + } + + /** + * @return the SQRT2 + */ + public static float getSQRT2() { + return SQRT2; + } + + /** + * @param aSQRT2 the SQRT2 to set + */ + public static void setSQRT2(float aSQRT2) { + SQRT2 = aSQRT2; + } + + /** + * @return the t + */ + public Texture getT() { + return t; + } + + /** + * @param t the t to set + */ + public void setT(Texture t) { + this.t = t; + } + + /** + * @return the playerSprite + */ + public AnimatedSprite getPlayerSprite() { + return playerSprite; + } + + /** + * @param playerSprite the playerSprite to set + */ + public void setPlayerSprite(AnimatedSprite playerSprite) { + this.playerSprite = playerSprite; + } + + /** + * @return the movementX + */ + public float getMovementX() { + return movementX; + } + + /** + * @param movementX the movementX to set + */ + public void setMovementX(float movementX) { + this.movementX = movementX; + } + + /** + * @return the movementY + */ + public float getMovementY() { + return movementY; + } + + /** + * @param movementY the movementY to set + */ + public void setMovementY(float movementY) { + this.movementY = movementY; + } + + /** + * @return the speed + */ + public float getSpeed() { + return speed; + } + + /** + * @param speed the speed to set + */ + public void setSpeed(float speed) { + this.speed = speed; + } + + /** + * @return the velocity + */ + public float getVelocity() { + return velocity; + } + + /** + * @param velocity the velocity to set + */ + public void setVelocity(float velocity) { + this.velocity = velocity; + } + + /** + * @return the facing + */ + public int getFacing() { + return facing; + } + + /** + * @param facing the facing to set + */ + public void setFacing(int facing) { + this.facing = facing; + } + + /** + * @return the quests + */ + public ArrayList getQuests() { + return quests; + } + + /** + * @param quests the quests to set + */ + public void setQuests(ArrayList quests) { + this.quests = quests; + } + + /** + * @return the collisionRect + */ + public Rectangle getCollisionRect() { + return collisionRect; + } + + /** + * @param collisionRect the collisionRect to set + */ + public void setCollisionRect(Rectangle collisionRect) { + this.collisionRect = collisionRect; + } + + /** + * @return the questGroup + */ + public Group getQuestGroup() { + return questGroup; + } + + /** + * @param questGroup the questGroup to set + */ + public void setQuestGroup(Group questGroup) { + this.questGroup = questGroup; + } + + /** + * @return the stats + */ + public Stats getStats() { + return stats; + } + + /** + * @param stats the stats to set + */ + public void setStats(Stats stats) { + this.stats = stats; + } + + +}