Skip to content

Commit

Permalink
Working NPC with AI (aimless random wandering) and a not finished dia…
Browse files Browse the repository at this point in the history
…logue box
  • Loading branch information
ccasey645 committed Apr 2, 2015
1 parent c4ca20b commit 38ea23c
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/core/assets/area/demo.txt
Expand Up @@ -34,5 +34,6 @@
5 8 2
5 7 2
5 6 2
0
1
8 8 2
8 7 100 10 1
3 changes: 2 additions & 1 deletion src/core/assets/img/entities.txt
@@ -1,2 +1,3 @@
1
2
1 img/player.png
2 img/player.png
11 changes: 8 additions & 3 deletions src/core/src/cs383/team1/Main.java
Expand Up @@ -5,6 +5,7 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import cs383.team1.input.DialogueBox;
import cs383.team1.input.InputManager;
import cs383.team1.model.GameManager;
import cs383.team1.render.DemoDisplay;
Expand All @@ -13,6 +14,7 @@ public class Main implements ApplicationListener, InputProcessor {
public InputManager inputManager;
public GameManager gm;
public DemoDisplay screen;
public DialogueBox textBox;

@Override
public void create () {
Expand All @@ -21,13 +23,15 @@ public void create () {
Gdx.input.setInputProcessor(this);

inputManager = new InputManager();
textBox = new DialogueBox();

This comment has been minimized.

Copy link
@fabe0940

fabe0940 Apr 2, 2015

Member

Maybe this should belong to a Display subclass? We're trying to keep Main as thin as possible, and let the rest of the classes do most (all) of the heavy lifting. It's just a thought, but a DialogueBox does not seem like a top level class nor does it seem like an independent one.


Gdx.app.debug("Main:create", "instantiating GameManager");
gm = GameManager.instance;

Gdx.app.debug("Main:create", "instantiating DemoDisplay");
screen = new DemoDisplay();
}
//screen = new DemoDisplay(textBox);
screen = new DemoDisplay();
}

@Override
public void dispose() {
Expand All @@ -39,7 +43,7 @@ public void dispose() {
public void render() {
if(inputManager.consumable()) {
Gdx.app.debug("Main:render", "Updating GameManager");
gm.update(inputManager);
gm.update(inputManager, textBox);

This comment has been minimized.

Copy link
@fabe0940

fabe0940 Apr 2, 2015

Member

See comments on the GameManager class.

}

screen.render();
Expand Down Expand Up @@ -71,6 +75,7 @@ public boolean keyUp (int key) {

@Override
public boolean keyTyped (char ch) {

return false;
}

Expand Down
55 changes: 55 additions & 0 deletions src/core/src/cs383/team1/input/DialogueBox.java
@@ -0,0 +1,55 @@
/* DialogueBox.java
Creates TextArea based on user input
*/
package cs383.team1.input;

import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextArea;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import java.lang.String;
import java.util.ArrayList;

/**
*
* @author Casey
*/
public class DialogueBox {
Skin skin = new Skin();
TextArea textBox;
public TextButton button;
String text;
public ArrayList<String> messages;
Label textDisplay;

public void DialogueBox(){
textBox = new TextArea("", skin);
textBox = new TextArea("", skin);
button = new TextButton("Send", skin);
button.addListener(new ClickListener());
messages = new ArrayList();
text = new String();
}

public void clicked(InputEvent event, float x, float y){

This comment has been minimized.

Copy link
@fabe0940

fabe0940 Apr 2, 2015

Member

We're treating positions as (int, int) pairs most places. There's even a Position object. Probably want to switch over for consistency?


if(textBox.getText().length() > 0){
text = textBox.getText();
}
messages.add(text);
textDisplay = new Label(messages.get(0), skin);
}

public void removeMessage(){
messages.remove(text);
}

public boolean consumable(){
return !messages.isEmpty();
}

}
17 changes: 13 additions & 4 deletions src/core/src/cs383/team1/model/GameManager.java
Expand Up @@ -3,10 +3,12 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.Input.Keys;
import cs383.team1.input.DialogueBox;
import cs383.team1.input.InputManager;
import cs383.team1.model.State;
import cs383.team1.model.StateManager;
import cs383.team1.model.overworld.AreaManager;
import cs383.team1.model.overworld.NPC;
import cs383.team1.model.overworld.Player;
import cs383.team1.model.overworld.Position;
import cs383.team1.model.overworld.Tile;
Expand Down Expand Up @@ -49,13 +51,18 @@ public void load() {
areas.current = index != -1 ? areas.areas.get(index) : null;
}

public void update(InputManager in) {
public void update(InputManager in, DialogueBox textBox) {

This comment has been minimized.

Copy link
@fabe0940

fabe0940 Apr 2, 2015

Member

Just a warning, with multiplayer/network code coming, we are going to be working very hard to decouple cs383.team1.model from cs383.team1.render and cs383.team1.input almost completely.

Doesn't make any difference for the moment, but in an ideal world cs383.team1.model is completely ignorant of the other modules. Just a head's up as far as where the code is eventually headed.

Player player;
Position next;
Tile target;

NPC npc;
player = areas.current.player;

npc = (NPC) areas.current.entities.get(0);
npc.ai(areas.current);

//while(textBox.consumable()){

//}
while(in.consumable()) {
switch(in.keys.remove(0)) {
case Keys.LEFT:
Expand All @@ -70,7 +77,7 @@ public void update(InputManager in) {
case Keys.DOWN:
next = new Position(player.pos.x, player.pos.y - 1);
break;
default:
default:
continue;
}

Expand All @@ -91,6 +98,8 @@ public void update(InputManager in) {
player.pos = next;
}
}



/* TODO: move the keyhandling code to the StateManager */
/*
Expand Down
2 changes: 2 additions & 0 deletions src/core/src/cs383/team1/model/overworld/Area.java
Expand Up @@ -10,6 +10,7 @@ public class Area {
public List<Tile> tiles;
public List<Entity> entities;
public Player player;


public Area() {
this(new ArrayList<Tile>(), new ArrayList<Entity>(), new Player());
Expand All @@ -20,5 +21,6 @@ public Area(List<Tile> tileList, List<Entity> entityList, Player p) {
tiles = tileList;
entities = entityList;
player = p;

}
}
6 changes: 5 additions & 1 deletion src/core/src/cs383/team1/model/overworld/AreaManager.java
Expand Up @@ -99,12 +99,16 @@ public Area loadArea(String fname) {
type = Integer.parseInt(vals[i + 2]);

pos = new Position(x, y);

switch(type) {
case DemoEntity.TYPE:
Gdx.app.debug("AreaManager:loadArea", "Loading DemoEntity");
entities.add(new DemoEntity(pos));
break;
case NPC.TYPE:
Gdx.app.debug("AreaManager:loadArea", "Loading NPC");
entities.add(new NPC(pos));
break;
default:
Gdx.app.error("AreaManager:loadArea",
"invalid entity type " + vals[i + 2]);
Expand Down
86 changes: 86 additions & 0 deletions src/core/src/cs383/team1/model/overworld/NPC.java
@@ -0,0 +1,86 @@
/*
* 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 cs383.team1.model.overworld;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import cs383.team1.model.overworld.Entity;
import cs383.team1.model.overworld.Position;
import cs383.team1.model.overworld.Tile;
import java.util.Random;

public final class NPC implements Entity {
public static final int TYPE = 2;
public Position pos;
Position next;
Tile target;

public int hp;
public int mp;
public int ap;


public NPC() {
//this(new Position(0, 0), 0, 0, 0);
this(new Position(0, 0));
}


public NPC(Position p) {
Gdx.app.debug("Player:Player", "instantiating class");

This comment has been minimized.

Copy link
@fabe0940

fabe0940 Apr 2, 2015

Member

Should be Gdx.app.debug("NPC:NPC", "instantiating class");

pos = p;
}

public int type() {
return TYPE;
}

public Position pos() {
return pos;
}

public void ai(Area area) {

This comment has been minimized.

Copy link
@fabe0940

fabe0940 Apr 2, 2015

Member

Probably should be update()? Most other things use update() functions, and having Area:update() delegate on to Entity:update() makes the most sense IMO...

Edit: Huh, Entity:update() isn't a thing yet. It should be, though.

Random randomDirection = new Random();
int randomint = randomDirection.nextInt(4);
System.out.println("Printing random direction: " + randomint);
switch(randomint) {
case 0:
//Move Left
next = new Position(pos.x - 1, pos.y);
break;
case 1:
//Move right
next = new Position(pos.x + 1, pos.y);
break;
case 2:
//Move up
next = new Position(pos.x, pos.y + 1);
break;
case 3:
//Move down
next = new Position(pos.x, pos.y - 1);
break;

}

target = null;
for(Tile t : area.tiles) {
if(t.pos().x == next.x && t.pos().y == next.y) {
target = t;
break;
}
}

if(target == null) {
Gdx.app.error("GameManager:update", "invalid move");

}

if(target.passable()) {
pos = next;
}
}
}
21 changes: 20 additions & 1 deletion src/core/src/cs383/team1/render/DemoDisplay.java
Expand Up @@ -6,14 +6,19 @@
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import cs383.team1.model.GameManager;
import cs383.team1.model.overworld.Entity;
import cs383.team1.model.overworld.Tile;
import cs383.team1.model.overworld.DemoEntity;
import cs383.team1.model.overworld.Player;
import cs383.team1.render.Display;
import cs383.team1.input.DialogueBox;

public class DemoDisplay extends Display {
private static final String FNAME = "img/demo.png";
Expand All @@ -24,7 +29,11 @@ public class DemoDisplay extends Display {
private Map<Integer, Texture> tileTextures;
private Map<Integer, String> entitySprites;
private Map<Integer, Texture> entityTextures;

private Table table;
Skin skin;
Label dialogue;
DialogueBox chatBox;

private Texture getTileTexture(int i) {
String fname;

Expand Down Expand Up @@ -133,6 +142,11 @@ public DemoDisplay() {
tileTextures = new HashMap<Integer, Texture>();
entitySprites = new HashMap<Integer, String>();
entityTextures = new HashMap<Integer, Texture>();
table = new Table();
skin = new Skin();
//dialogue = new Label(textBox.messages.get(0), skin);
//chatBox = textBox;


loadSpriteMaps();
}
Expand Down Expand Up @@ -178,7 +192,12 @@ public void render() {
sprite.setPosition(player.pos().x * Tile.WIDTH,
(player.pos().y * Tile.HEIGHT) + (int) (0.33 * Tile.HEIGHT));
sprite.draw(batch);

//Draw dialogue text on to screen
//table.add(dialogue).padBottom(40).row();
//table.add(chatBox.button).size(150,60).padBottom(20).row();


batch.end();
}
}

0 comments on commit 38ea23c

Please sign in to comment.