diff --git a/core/assets/map1.tmx b/core/assets/map1.tmx index c710ca8..2f665f8 100644 --- a/core/assets/map1.tmx +++ b/core/assets/map1.tmx @@ -1,5 +1,5 @@ - + @@ -290,7 +290,14 @@ - + + + + + + + + diff --git a/core/assets/sign.png b/core/assets/sign.png new file mode 100644 index 0000000..7387c5d Binary files /dev/null and b/core/assets/sign.png differ diff --git a/core/src/com/trs/main/AnimatedSprite.java b/core/src/com/trs/main/AnimatedSprite.java index 2e6d6c9..9bd78c8 100644 --- a/core/src/com/trs/main/AnimatedSprite.java +++ b/core/src/com/trs/main/AnimatedSprite.java @@ -11,7 +11,7 @@ import com.badlogic.gdx.graphics.g2d.TextureRegion; */ public class AnimatedSprite { - private final static int[] ROWLENGTHS = {7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 6, 6, 6, 12, 12, 12, 12, 6}; + private int[] rowlengths = {7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 6, 6, 6, 12, 12, 12, 12, 6}; private Sprite sprite; private TextureRegion[][] texture; @@ -19,13 +19,20 @@ public class AnimatedSprite { private int row; private float delta; - public AnimatedSprite(Texture tx, int tileWidth, int tileHeight){ + public AnimatedSprite(Texture tx, int tileWidth, int tileHeight, boolean isPlayer){ texture = TextureRegion.split(tx, tileWidth, tileHeight); sprite = new Sprite(); row = (int) (Math.random()*texture.length); frame = (int) (Math.random()*texture[row].length); + if(!isPlayer) { + rowlengths = new int[tx.getHeight() / tileHeight]; + for(int i = 0; i < rowlengths.length; i++) { + rowlengths[i] = texture[i].length; + } + } + sprite = new Sprite(texture[getRow()][getFrame()]); } @@ -34,7 +41,7 @@ public class AnimatedSprite { if(this.delta >= 0.1f) { this.delta = 0; - if(getFrame() >= ROWLENGTHS[getRow()] - 1){ + if(getFrame() >= rowlengths[getRow()] - 1){ setFrame(0); } else{ diff --git a/core/src/com/trs/main/InteractionObject.java b/core/src/com/trs/main/InteractionObject.java new file mode 100644 index 0000000..cd1a47e --- /dev/null +++ b/core/src/com/trs/main/InteractionObject.java @@ -0,0 +1,104 @@ +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.Intersector; +import com.badlogic.gdx.math.Rectangle; +import com.badlogic.gdx.scenes.scene2d.Actor; + +public class InteractionObject extends Actor{ + Textbox t; + Rectangle collisionRect; + private AnimatedSprite animatedSprite; + + boolean currentlyTalking; + DialogueParser parser; + + int id; + + public InteractionObject(Rectangle collisionRect, float xPos, float yPos, int id, String texture){ + setName("interactive"); + this.id = id; + Texture t = new Texture(Gdx.files.internal(texture)); + + currentlyTalking = false; + + animatedSprite = new AnimatedSprite(t, 32, 32, false); + animatedSprite.setRow(0); + this.collisionRect = collisionRect; + + parser = new DialogueParser("npcs/1/dialogue/test.txt"); + Dialogue nextDialogue = parser.firstDialogue(); + this.t = new Textbox(nextDialogue.question, nextDialogue.ans, getX()+getWidth()/2, getY()+getHeight()/2); + + setBounds(xPos, yPos, animatedSprite.getSprite().getWidth(), animatedSprite.getSprite().getHeight()); + } + + public void startDialogue(float xPos, float yPos) { + currentlyTalking = true; + getStage().addActor(new Textbox(t, xPos, yPos)); + } + + @Override + protected void positionChanged() { + animatedSprite.setSpritePosition((int)getX(), (int)getY()); + collisionRect = new Rectangle(getX() + 16, getY(), 32, 48); + super.positionChanged(); + } + + @Override + public void act(float delta) { + if(Main.gamestate == 1 && currentlyTalking) { + if(currentlyTalking) { + for(Actor a : getStage().getActors().toArray(Actor.class)) { + if(a instanceof Textbox) { + if(((Textbox) a).getState() == 2) { + int answer = ((Textbox) a).getSelectedAsw(); + Dialogue newDialogue = parser.nextDialogue(answer + 1); + + if(newDialogue == null) { + currentlyTalking = false; + parser = new DialogueParser("npcs/1/dialogue/test.txt"); + System.out.println("asdfasdf"); + } + else { + ((Textbox)a).update(newDialogue); + System.out.println("update nicencie"); + } + } + } + } + } + } + + animatedSprite.updateAnimation(delta); + + } + + @Override + public void draw(Batch batch, float parentAlpha) { + animatedSprite.draw(batch); + super.draw(batch, parentAlpha); //To change body of generated methods, choose Tools | Templates. + } + + public boolean collidingWithMapCollisionObject(){ + boolean value = false; + for(Actor a : getStage().getActors()){ + if(a.getName().equals("mapobject")){ + //Rectangle p = new Rectangle(getX(), getY(), getWidth(), getHeight()); + Rectangle o = new Rectangle(a.getX(), a.getY(), a.getWidth(), a.getHeight()); + if(Intersector.overlaps(collisionRect, o)){ + value = true; + break; + } + } + } + return value; + } + + public Textbox getTextbox(){ + return t; + } + +} diff --git a/core/src/com/trs/main/MapContainer.java b/core/src/com/trs/main/MapContainer.java index 0b404ef..85c4383 100644 --- a/core/src/com/trs/main/MapContainer.java +++ b/core/src/com/trs/main/MapContainer.java @@ -40,13 +40,13 @@ public class MapContainer { Stage stage; OrthographicCamera camera; TmxMapLoader maploader; - TiledMap map; - OrthogonalTiledMapRenderer renderer; + TiledMap map; + OrthogonalTiledMapRenderer renderer; Door[] doors; public Door collidingDoor; - final int[] layersBelowPlayer = {0, 1, 2}; - final int[] layersAbovePlayer = {3, 4}; + final int[] layersBelowPlayer = {0, 1, 2}; + final int[] layersAbovePlayer = {3, 4}; // TODO: Value which shows from which door the player is coming? public MapContainer(float CAMERA_WIDTH, float CAMERA_HEIGHT, Player p, String mapString, int inDoor) { @@ -96,6 +96,18 @@ public class MapContainer { stage.addActor(new MovingNpc(rect, rect.getX() + (float)(Math.random()*rect.getWidth()), rect.getY()+(float)(Math.random()*rect.getHeight()), id)); } + // adding the InteractionObjects + for(MapObject object : map.getLayers().get(7).getObjects().getByType(RectangleMapObject.class)){ + Rectangle rect = ((RectangleMapObject) object).getRectangle(); + MapProperties props = object.getProperties(); + + int id = props.get("id", Integer.class); + String texture = props.get("texture", String.class); + + stage.addActor(new InteractionObject(rect, rect.getX(), rect.getY(), id, texture)); + stage.addActor(new MapCollisionObject((int) rect.x, (int) rect.y, (int) rect.width, (int) rect.height)); + } + doors = new Door[tempDoors.size()]; for(int i = 0; i < doors.length; i++) { doors[i] = tempDoors.get(i); diff --git a/core/src/com/trs/main/MovingNpc.java b/core/src/com/trs/main/MovingNpc.java index eaf8d6b..891d128 100644 --- a/core/src/com/trs/main/MovingNpc.java +++ b/core/src/com/trs/main/MovingNpc.java @@ -44,7 +44,7 @@ public class MovingNpc extends Actor{ currentlyTalking = false; - animatedSprite = new AnimatedSprite(t, 64, 64); + animatedSprite = new AnimatedSprite(t, 64, 64, true); animatedSprite.setRow(0); collisionRect = new Rectangle(xPos + 16, yPos, 32, 48); this.area = area; diff --git a/core/src/com/trs/main/Player.java b/core/src/com/trs/main/Player.java index c86f732..a09f4f6 100644 --- a/core/src/com/trs/main/Player.java +++ b/core/src/com/trs/main/Player.java @@ -35,9 +35,9 @@ public class Player extends Actor{ public Player(int xPos, int yPos){ setName("player"); t = new Texture(Gdx.files.internal("player.png")); - playerSprite = new AnimatedSprite(t, 64, 64); + playerSprite = new AnimatedSprite(t, 64, 64, true); playerSprite.setRow(0); - collisionRect = new Rectangle(xPos + 16, yPos, 32, 48); + collisionRect = new Rectangle(xPos + 16, yPos, 32, 16); setBounds(xPos, yPos, playerSprite.getSprite().getWidth(), playerSprite.getSprite().getHeight()); } @@ -79,11 +79,20 @@ public class Player extends Actor{ } if(Gdx.input.isKeyJustPressed(Input.Keys.E)) { Actor a = collidingActor(); - if(a != null && a instanceof MovingNpc){ - Main.gamestate = 1; - ((MovingNpc)a).startDialogue(getX()+32, getY()+32); - movementX = 0; - movementY = 0; + if(a != null) { + if(a instanceof MovingNpc){ + Main.gamestate = 1; + ((MovingNpc)a).startDialogue(getX()+32, getY()+32); + movementX = 0; + movementY = 0; + } + else if(a instanceof InteractionObject) { + System.out.println("kfdjfkdjfladjflajdfjadlfj"); + Main.gamestate = 1; + ((InteractionObject)a).startDialogue(getX()+32, getY()+32); + movementX = 0; + movementY = 0; + } } } } @@ -150,7 +159,7 @@ public class Player extends Actor{ public boolean collidingWithMapCollisionObject(){ boolean value = false; for(Actor a : getStage().getActors()){ - if(a.getName().equals("mapobject")){ + if(a instanceof MapCollisionObject){ //Rectangle p = new Rectangle(getX(), getY(), getWidth(), getHeight()); Rectangle o = new Rectangle(a.getX(), a.getY(), a.getWidth(), a.getHeight()); if(Intersector.overlaps(collisionRect, o)){ @@ -192,7 +201,7 @@ public class Player extends Actor{ public Actor collidingActor(){ for(Actor a : getStage().getActors()){ - if(a.getName().equals("npc")){ + 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)){