From 758a4cf55cfa229330fa2f69db5f881b22e30e09 Mon Sep 17 00:00:00 2001 From: GammelJAN Date: Tue, 22 Dec 2020 13:35:42 +0100 Subject: [PATCH] Dialogues working --- core/assets/dialogues/test.txt | 28 +++++++++---- core/src/com/trs/main/DialogueParser.java | 17 +++++--- core/src/com/trs/main/MapContainer.java | 8 ++-- core/src/com/trs/main/MovingNpc.java | 41 ++++++++++--------- core/src/com/trs/main/Player.java | 3 +- core/src/com/trs/main/Textbox.java | 23 ++++++----- .../trs/main/view/screens/AbstractScreen.java | 5 --- .../com/trs/main/view/screens/GameScreen.java | 18 -------- 8 files changed, 70 insertions(+), 73 deletions(-) diff --git a/core/assets/dialogues/test.txt b/core/assets/dialogues/test.txt index 430d944..f29f676 100644 --- a/core/assets/dialogues/test.txt +++ b/core/assets/dialogues/test.txt @@ -1,11 +1,21 @@ -Ultra geile ultra Frage -Antwort 2#5 -Antwort 7#9 +Wie gehts +gut#5 +schlecht#9 -Frage von Antwort 2 -Nice#-1 -Nice2#-1 +Das freut mich +wie gehts dir#20 +tschüss#-1 -Frage von Antwort 7 -lolol#-1 -lolol#-1 +Das tut mir leid. Wieso denn? +Spaß mir gehts gut#5 +tschüss#-1 +Halts Maul#14 + +Ey es gibt gleich Stress hier +cool, bin ich dabei#17 + +KAMPF +LOS#-1 + +Ich will Krieg +KRIEG!#17 \ No newline at end of file diff --git a/core/src/com/trs/main/DialogueParser.java b/core/src/com/trs/main/DialogueParser.java index 0e393ba..8b43da1 100644 --- a/core/src/com/trs/main/DialogueParser.java +++ b/core/src/com/trs/main/DialogueParser.java @@ -34,7 +34,8 @@ public class DialogueParser { break; } else { - ans.add(tempAns); + String[] split = tempAns.split("#"); + ans.add(split[0]); } } String[] answers = new String[ans.size()]; @@ -52,7 +53,7 @@ public class DialogueParser { String[] newLine = s.split("#"); line = Integer.parseInt(newLine[1]) - 1; - if(line == -1) { + if(line < 0) { return null; } @@ -64,11 +65,17 @@ public class DialogueParser { break; } else { - ans.add(tempAns); + String[] split = tempAns.split("#"); + ans.add(split[0]); } } - result.ans = (String[]) ans.toArray(); - + + String[] answer = new String[ans.size()]; + for(int i = 0; i < ans.size(); i++){ + answer[i] = ans.get(i); + } + result.ans = answer; + return result; } } diff --git a/core/src/com/trs/main/MapContainer.java b/core/src/com/trs/main/MapContainer.java index dd4b57e..c08a8d0 100644 --- a/core/src/com/trs/main/MapContainer.java +++ b/core/src/com/trs/main/MapContainer.java @@ -122,22 +122,20 @@ public class MapContainer { if(Main.gamestate == 1) { Textbox t = null; for(Actor a : stage.getActors()){ - if(a.getName().equals("textbox")){ + if(a instanceof Textbox){ t = (Textbox)a; - if(t.getState() == 2){ + if(t.getState() == 3){ a.remove(); - System.out.println("aösdkjf"); Main.gamestate = 0; t.getSelectedAsw(); // DO STUFF NICENICE } } } - System.out.println(); } // center camera for(Actor a : stage.getActors()){ - if(a.getName().equals("player")){ + if(a instanceof Player){ stage.getCamera().position.set((a.getX()+a.getWidth()/2), (a.getY()+a.getHeight()/2), 0); stage.getCamera().update(); break; diff --git a/core/src/com/trs/main/MovingNpc.java b/core/src/com/trs/main/MovingNpc.java index ccd4034..d7875c4 100644 --- a/core/src/com/trs/main/MovingNpc.java +++ b/core/src/com/trs/main/MovingNpc.java @@ -138,25 +138,28 @@ public class MovingNpc extends Actor{ movementY = 0; } else if(Main.gamestate == 1) { - animatedSprite.setRow(facing); - - if(currentlyTalking) { - for(Actor a : getStage().getActors()) { - if(a instanceof Textbox) { - if(((Textbox) a).state == 2) { - int answer = ((Textbox) a).getSelectedAsw(); - Dialogue newDialogue = parser.nextDialogue(answer + 1); - - if(newDialogue == null) { - currentlyTalking = false; - } - else { - t.update(newDialogue); - } - } - } - } - } + animatedSprite.setRow(facing); + + 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("dialogues/test.txt"); + System.out.println("asdfasdf"); + } + else { + ((Textbox)a).update(newDialogue); + System.out.println("update nicencie"); + } + } + } + } + } } animatedSprite.updateAnimation(delta); diff --git a/core/src/com/trs/main/Player.java b/core/src/com/trs/main/Player.java index 94042fb..21473e7 100644 --- a/core/src/com/trs/main/Player.java +++ b/core/src/com/trs/main/Player.java @@ -84,8 +84,7 @@ public class Player extends Actor{ Actor a = collidingActor(); if(a != null && a instanceof MovingNpc){ Main.gamestate = 1; - Textbox t = ((MovingNpc)a).getTextbox(); - getStage().addActor(new Textbox(t, getX()+32, getY()+32)); + ((MovingNpc)a).startDialogue(getX()+32, getY()+32); movementX = 0; movementY = 0; } diff --git a/core/src/com/trs/main/Textbox.java b/core/src/com/trs/main/Textbox.java index 5a9b285..563c572 100644 --- a/core/src/com/trs/main/Textbox.java +++ b/core/src/com/trs/main/Textbox.java @@ -79,8 +79,8 @@ public class Textbox extends Actor{ textHeight = getTextHeight("A"); this.splitted = t.splitted; this.ans = t.ans; - System.out.println(splitted.size()); - float height = this.splitted.size() * 1.2f * textHeight + (this.ans.length+2) * 1.2f * textHeight; + //System.out.println(splitted.size()); + float height = this.splitted.size() * 1.2f * textHeight + (this.ans.length+1) * 1.2f * textHeight; r = new Rectangle(xPos - Main.CAMERA_WIDTH/2 + 20, yPos - Main.CAMERA_HEIGHT/2 + 20, Main.CAMERA_WIDTH - 40, height); setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight()); @@ -95,13 +95,13 @@ public class Textbox extends Actor{ this.splitted = getSplitted(d.question, (int) Main.CAMERA_WIDTH / 2); this.ans = d.ans; - float height = this.splitted.size() * 1.2f * textHeight + (this.ans.length+2) * 1.2f * textHeight; - r = new Rectangle(getX(), getY(), Main.CAMERA_WIDTH - 40, height); - setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight()); - - this.state = 0; - printLine = 0; - printChar = 0; + float height = this.splitted.size() * 1.2f * textHeight + (this.ans.length+1) * 1.2f * textHeight; + r = new Rectangle(getX(), getY(), getWidth(), height); + setBounds(r.getX(), r.getY(), r.getWidth(), r.getHeight()); + this.state = 0; + printLine = 0; + printChar = 0; + selectedAsw = 0; } @Override @@ -127,6 +127,9 @@ public class Textbox extends Actor{ System.out.println(ans[selectedAsw]); } } + else if(state == 2){ + state = 3; + } else{ if(printChar >= splitted.get(printLine).length()){ if(splitted.size()-1 <= printLine){ @@ -195,7 +198,7 @@ public class Textbox extends Actor{ font.setColor(Color.BLACK); } - font.draw(batch, ans[i], getX()+5, getY() + getHeight() - ((splitted.size() + i + 1) * 1.2f * textHeight - 5)); + font.draw(batch, ans[i], getX()+20, getY() + getHeight() - ((splitted.size() + i + 1) * 1.2f * textHeight - 5)); } } super.draw(batch, parentAlpha); diff --git a/core/src/com/trs/main/view/screens/AbstractScreen.java b/core/src/com/trs/main/view/screens/AbstractScreen.java index 26a71fb..1949ea4 100644 --- a/core/src/com/trs/main/view/screens/AbstractScreen.java +++ b/core/src/com/trs/main/view/screens/AbstractScreen.java @@ -6,12 +6,7 @@ package com.trs.main.view.screens; import com.badlogic.gdx.Game; -import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Screen; -import com.badlogic.gdx.graphics.OrthographicCamera; -import com.badlogic.gdx.scenes.scene2d.Stage; -import com.badlogic.gdx.utils.viewport.FitViewport; -import com.trs.main.Textbox; /** * diff --git a/core/src/com/trs/main/view/screens/GameScreen.java b/core/src/com/trs/main/view/screens/GameScreen.java index 8db4c87..918f1b3 100644 --- a/core/src/com/trs/main/view/screens/GameScreen.java +++ b/core/src/com/trs/main/view/screens/GameScreen.java @@ -6,27 +6,9 @@ package com.trs.main.view.screens; import com.badlogic.gdx.Game; -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.Input; -import com.badlogic.gdx.graphics.OrthographicCamera; -import com.badlogic.gdx.maps.MapObject; -import com.badlogic.gdx.maps.objects.RectangleMapObject; -import com.badlogic.gdx.maps.tiled.TiledMap; -import com.badlogic.gdx.maps.tiled.TmxMapLoader; -import com.badlogic.gdx.maps.tiled.objects.TiledMapTileMapObject; -import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer; -import com.badlogic.gdx.math.Rectangle; -import com.badlogic.gdx.math.Vector3; -import com.badlogic.gdx.physics.box2d.BodyDef; -import com.badlogic.gdx.scenes.scene2d.Actor; -import com.badlogic.gdx.scenes.scene2d.Group; import com.trs.main.Main; -import com.trs.main.MapCollisionObject; import com.trs.main.MapContainer; -import com.trs.main.MovingNpc; import com.trs.main.Player; -import com.trs.main.Textbox; -import java.util.ArrayList; /** *