Initial commit

This commit is contained in:
Jalen Winslow
2017-11-06 10:55:21 -07:00
commit a4a77fb8dd
36 changed files with 740 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
package build.com.hobogames;
import build.com.hobogames.states.*;
public class Game {
private boolean gameOn;
private Handler handler;
private State menuState;
private State playState;
public Game() {
}
public void init() {
gameOn = true;
handler = new Handler(this);
menuState = new MenuState(handler);
playState = new PlayState(handler);
State.setCurrentState(menuState);
State.getCurrentState().init();
}
public void gameLoop() {
init();
while (gameOn) {
if (State.getCurrentState() != null) {
State.getCurrentState().update();
}
System.out.println("\n----------------\n");
}
end();
}
public void end() {
System.out.println("Exiting game.");
}
public boolean getGameOn() {return gameOn;}
public MenuState getMenuState() {return (MenuState)menuState;}
public PlayState getPlayState() {return (PlayState)playState;}
public void setGameOn(boolean gameOn) {this.gameOn = gameOn;}
}

View File

@@ -0,0 +1,26 @@
package build.com.hobogames;
import build.com.hobogames.states.*;
import build.com.hobogames.gameobj.entities.humans.Player;
import java.util.Random;
import java.util.Scanner;
public class Handler {
private Game game;
public Random rand;
public Scanner sc;
public Handler(Game game) {
this.game = game;
this.rand = new Random();
sc = new Scanner(System.in);
}
public Game getGame() {return game;}
public boolean getGameOn() {return game.getGameOn();}
public MenuState getMenuState() {return game.getMenuState();}
public PlayState getPlayState() {return game.getPlayState();}
public Player getPlayer() {return getPlayState().getPlayer();}
}

View File

@@ -0,0 +1,23 @@
package build.com.hobogames.gameobj;
import build.com.hobogames.Handler;
import build.com.hobogames.gameobj.GameObject;
import java.util.ArrayList;
public class GOHandler {
private Handler handler;
private ArrayList<GameObject> gameObjects;
public GOHandler(Handler handler) {
this.handler = handler;
}
public void update() {
for (int i = 0; i < gameObjects.size(); i++) {
if (gameObjects.get(i) != null) {
gameObjects.get(i).update();
}
}
}
}

View File

@@ -0,0 +1,35 @@
package build.com.hobogames.gameobj;
import build.com.hobogames.Handler;
public abstract class GameObject {
protected Handler handler;
protected int posX, posY;
protected String name;
protected int gameId;
public static int idNum = 0;
public GameObject(Handler handler) {
this.handler = handler;
posX = 0;
posY = 0;
idNum++;
gameId = idNum;
}
public abstract void init();
public abstract void update();
public abstract void end();
public int getPosX() {return posX;}
public int getPosY() {return posY;}
public String getName() {return name;}
public int getGameId() {return gameId;}
public void setPosX(int posX) {this.posX = posX;}
public void setPosY(int posY) {this.posY = posY;}
public void setName(String name) {this.name = name;}
public void setGameId(int id) {this.gameId = id;}
}

View File

@@ -0,0 +1,65 @@
package build.com.hobogames.gameobj.entities;
import build.com.hobogames.gameobj.GameObject;
import build.com.hobogames.Handler;
public abstract class Entity extends GameObject {
protected int dir;
protected int health, maxHealth;
protected int stamina, maxStamina;
protected int attack, defense, speed;
protected int blockChance, dodgeChance, hitChance;
protected int xp, level;
//attributes
protected int strength, agility, endurance, intelligence, perception, charisma, luck;
//protected Trait[] traits = new Trait[40]
//protected Skill[] skills = new Skills[20]
public Entity(Handler handler) {
super(handler);
}
public abstract void move(int moveX, int moveY);
public abstract void attack();
public int getDir() {return dir;}
public int getHealth() {return health;}
public int getMaxHealth() {return maxHealth;}
public int getAttack() {return attack;}
public int getDefense() {return defense;}
public int getSpeed() {return speed;}
public int getBlockChance() {return blockChance;}
public int getDodgeChance() {return dodgeChance;}
public int getHitChance() {return hitChance;}
public int getXp() {return xp;}
public int getLevel() {return level;}
public int getStrength() {return strength;}
public int getAgility() {return agility;}
public int getEndurance() {return endurance;}
public int getIntelligence() {return intelligence;}
public int getPerception() {return perception;}
public int getCharisma() {return charisma;}
public int getLuck() {return luck;}
public void setDir(int dir) {this.dir = dir;}
public void setHealth(int health) {this.health = health;}
public void setMaxHealth(int maxHealth) {this.maxHealth = maxHealth;}
public void setAttack(int attack) {this.attack = attack;}
public void setDefense(int defense) {this.defense = defense;}
public void setSpeed(int speed) {this.speed = speed;}
public void setBlockChance(int blockChance) {this.blockChance = blockChance;}
public void setDodgeChance(int dodgeChance) {this.dodgeChance = dodgeChance;}
public void setHitChance(int hitChance) {this.hitChance = hitChance;}
public void setXp(int xp) {this.xp = xp;}
public void setLevel(int level) {this.level = level;}
public void setStrength(int strength) {this.strength = strength;}
public void setAgility(int agility) {this.agility = agility;}
public void setEndurance(int endurance) {this.endurance = endurance;}
public void setIntelligence(int intelligence) {this.intelligence = intelligence;}
public void setPerception(int perception) {this.perception = perception;}
public void setCharisma(int charisma) {this.charisma = charisma;}
public void setLuck(int luck) {this.luck = luck;}
}

View File

@@ -0,0 +1,38 @@
package build.com.hobogames.gameobj.entities.humans;
import build.com.hobogames.Handler;
import build.com.hobogames.gameobj.entities.Entity;
public class Human extends Entity {
public Human(Handler handler) {
super(handler);
}
@Override
public void init() {
}
@Override
public void update() {
}
@Override
public void end() {
}
@Override
public void move(int moveX, int moveY) {
}
@Override
public void attack() {
}
}

View File

@@ -0,0 +1,46 @@
package build.com.hobogames.gameobj.entities.humans;
import build.com.hobogames.gameobj.entities.humans.Human;
import build.com.hobogames.Handler;
public class Player extends Human {
public Player(Handler handler) {
super(handler);
}
@Override
public void init() {
dir = handler.rand.nextInt(4);
maxHealth = 20; health = maxHealth;
maxStamina = 20; stamina = 20;
posX = 0; posY = 0;
name = "Player";
System.out.println(dir + " " + health + " " + stamina + " " + posX + posY + " " + name + " " + gameId);
System.out.println("Player is created!");
}
@Override
public void update() {
}
@Override
public void end() {
}
@Override
public void move(int moveX, int moveY) {
posX += moveX;
posY += moveY;
System.out.println("Player X: " + posX + " Y: " + posY);
}
@Override
public void attack() {
}
}

View File

@@ -0,0 +1,28 @@
package build.com.hobogames.gameobj.nonentities;
import build.com.hobogames.Handler;
import build.com.hobogames.gameobj.GameObject;
public class Wall extends GameObject{
public Wall(Handler handler) {
super(handler);
}
@Override
public void init() {
}
@Override
public void update() {
}
@Override
public void end() {
}
}

View File

@@ -0,0 +1,60 @@
package build.com.hobogames.states;
import build.com.hobogames.utils.Choice;
import build.com.hobogames.utils.ReadFile;
import build.com.hobogames.Handler;
public class MenuState extends State {
public MenuState(Handler handler) {
super(handler);
}
@Override
public void init() {
subState = "Main";
}
@Override
public void update() {
switch (subState) {
case "Main":
ReadFile.setPath("res/texts/Game1_menuScreen.txt");
System.out.print(ReadFile.getText() + "\n -> ");
Choice[] menuChoices = {
new Choice("Play", new String[]{"1", "Play", "Play Game", "p"}),
new Choice("Exit", new String[]{"2", "Exit", "Exit Game", "q"})
};
String choice = sc.nextLine();
for (int i = 0; i < menuChoices.length; i++) {
if (menuChoices[i].checkAnswer(choice)) {
subState = menuChoices[i].getAnswer();
break;
}
subState = choice;
}
break;
case "Play":
end();
State.setCurrentState(handler.getPlayState());
State.getCurrentState().init();
break;
case "Exit":
if (handler.getGameOn()) {handler.getGame().setGameOn(false);}
break;
default:
System.out.println("String:subState does not match any choices. Setting to main.");
subState = "Main";
break;
}
}
@Override
public void end() {
}
}

View File

@@ -0,0 +1,43 @@
package build.com.hobogames.states;
import build.com.hobogames.Handler;
import build.com.hobogames.gameobj.entities.humans.Player;
import build.com.hobogames.utils.Controller;
import build.com.hobogames.utils.Map;
public class PlayState extends State {
private Player player;
private Controller controls;
private Map map;
public PlayState(Handler handler) {
super(handler);
}
@Override
public void init() {
System.out.println("The game is not ready.");
player = new Player(handler);
controls = new Controller(handler);
map = new Map(handler);
player.init();
controls.init();
map.init();
}
@Override
public void update() {
controls.update();
map.displayMap();
}
@Override
public void end() {
}
public Player getPlayer() {return player;}
public void setPlayer(Player player) {this.player = player;}
}

View File

@@ -0,0 +1,25 @@
package build.com.hobogames.states;
import build.com.hobogames.Handler;
import java.util.Scanner;
public abstract class State {
protected static State currentState = null;
protected Handler handler;
protected Scanner sc = new Scanner(System.in);
protected String subState;
public State(Handler handler) {
this.handler = handler;
}
public abstract void init();
public abstract void update();
public abstract void end();
public static State getCurrentState() {return currentState;}
public static void setCurrentState(State state) {State.currentState = state;}
}

View File

@@ -0,0 +1,32 @@
package build.com.hobogames.utils;
public class Choice {
private String answer;
private String[] choices;
public Choice() {
answer = "";
choices = new String[1];
}
public Choice(String answer, String[] choices) {
this.answer = answer;
this.choices = new String[choices.length];
for (int i=0; i < choices.length; i++) {
this.choices[i] = choices[i];
}
}
public boolean checkAnswer(String choice) {
for (String c : choices) {
if (choice.equalsIgnoreCase(c)) {
return true;
}
}
return false;
}
public String getAnswer() {return answer;}
}

View File

@@ -0,0 +1,54 @@
package build.com.hobogames.utils;
import build.com.hobogames.Handler;
import build.com.hobogames.utils.Choice;
import build.com.hobogames.states.State;
public class Controller {
private Handler handler;
private Choice[] moveChoice;
public Controller(Handler handler) {
this.handler = handler;
}
public void init() {
moveChoice = new Choice[]{
new Choice("move north", new String[]{"move north", "north", "walk north", "n"}),
new Choice("move east", new String[]{"move east", "east", "walk east", "e"}),
new Choice("move south", new String[]{"move south", "south", "walk south", "s"}),
new Choice("move west", new String[]{"move west", "west", "walk west", "w"}),
new Choice("quit", new String[]{"quit", "q"})
};
}
public void update() {
System.out.print(" -> ");
String choice = handler.sc.nextLine();
for (Choice c : moveChoice) {
if (c.checkAnswer(choice)) {
choice = c.getAnswer();
}
}
switch (choice) {
case "move north": handler.getPlayer().move(0,1);break;
case "move east":handler.getPlayer().move(1,0);break;
case "move south": handler.getPlayer().move(0,-1);break;
case "move west": handler.getPlayer().move(-1,0);break;
case "quit":
System.out.println("Quiting the game");
handler.getPlayState().end();
State.setCurrentState(handler.getMenuState());
State.getCurrentState().init();
break;
default:
System.out.println("Unknown choice.");
}
}
}

View File

@@ -0,0 +1,90 @@
package build.com.hobogames.utils;
import build.com.hobogames.Handler;
import java.lang.Integer;
public class Map {
private Handler handler;
private String[][] mapData;
public Map(Handler handler) {
this.handler = handler;
}
public void init() {
buildMap();
}
public void buildMap() {
ReadFile.setPath("res/maps/Game1_tutorialMap.txt");
String[] mapFile = ReadFile.getText().split("\n");
mapData = new String[Integer.parseInt(mapFile[0].split(" ")[1])][Integer.parseInt(mapFile[0].split(" ")[0])];
for (int k = 0; k < mapFile.length; k++) {
String[] object = mapFile[k].split(" ");
switch (object[0]) {
case "room":
for (int j = 0; j < Integer.parseInt(object[4]); j++) {
for (int i = 0; i < Integer.parseInt(object[3]); i++) {
mapData[j + Integer.parseInt(object[2])][i + Integer.parseInt(object[1])] = ".";
}
}
break;
case "hall":
for (int j = 0; j < Integer.parseInt(object[4]); j++) {
for (int i = 0; i < Integer.parseInt(object[3]); i++) {
mapData[j + Integer.parseInt(object[2])][i + Integer.parseInt(object[1])] = ".";
}
}
break;
case "switch":
mapData[Integer.parseInt(object[2])][Integer.parseInt(object[1])] = "S";
break;
case "lock_door":
mapData[Integer.parseInt(object[2])][Integer.parseInt(object[1])] = "D";
break;
case "exit":
mapData[Integer.parseInt(object[2])][Integer.parseInt(object[1])] = "E";
break;
case "enemy":
mapData[Integer.parseInt(object[2])][Integer.parseInt(object[1])] = "@";
break;
case "pillar":
mapData[Integer.parseInt(object[2])][Integer.parseInt(object[1])] = "#";
break;
case "door":
mapData[Integer.parseInt(object[2])][Integer.parseInt(object[1])] = "D";
break;
case "p_plate":
mapData[Integer.parseInt(object[2])][Integer.parseInt(object[1])] = "^";
break;
}
}
for (int j = 0; j < mapData.length; j++) {
for (int i = 0; i < mapData[j].length; i++) {
if (mapData[j][i] == null) {
if (j+1 != mapData.length && i+1 != mapData[j].length &&
(mapData[j+1][i] != null || mapData[j][i+1] != null)) {
mapData[j][i] = "#";
} else if (j-1 != -1 && i-1 != -1 &&
(mapData[j-1][i] != null || mapData[j][i-1] != null) &&
(mapData[j-1][i] != "#" || mapData[j][i-1] != "#")) {
mapData[j][i] = "#";
} else
mapData[j][i] = " ";
}
}
}
}
public void displayMap() {
for (int j = 0; j < mapData.length; j++) {
for (int i = 0; i < mapData[j].length; i++) {
System.out.print(" " + mapData[j][i]);
}
System.out.println();
}
}
}

View File

@@ -0,0 +1,41 @@
package build.com.hobogames.utils;
import java.io.BufferedReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.File;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
public class ReadFile {
private static File fileToRead;
private static Path pathToFile;
public ReadFile() {
}
public static void setPath(String path) {
pathToFile = Paths.get(path);
fileToRead = pathToFile.toFile();
}
public static String getText() {
String line = "";
String text = "";
try (BufferedReader reader = new BufferedReader(new FileReader(fileToRead))) {
line = reader.readLine();
while (line != null) {
text += line + "\n";
line = reader.readLine();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return text;
}
}