Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basis for changing characters based on actions. #187

Merged
merged 8 commits into from
Dec 22, 2014
23 changes: 23 additions & 0 deletions json/character_transitions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"Sewer Rat": {
"kill": {
"Brotherhood Member": {
"Sewer Rat": -1,
"Brotherhood Member": -1,
"Syndicate Member": 1
},
"Syndicate Member": {
"Sewer Rat": -1,
"Brotherhood Member": 1,
"Syndicate Member": -1
},
"Recruit": {
"Recruit": -1
}
}
},
"Recruit": {
"kill": {
}
}
}
56 changes: 50 additions & 6 deletions json/npcs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
"npcs": {
"guide": {
"name": "Guide",
"health":100,
"healthMax": 100,
"damage": 1000,
"armour": 0,
"level": 1,
"xp": 0,
"strength": 0,
"intelligence": 0,
"dexterity": 0,
"stealth": 0,

"relationships": [
{
"recruit": "ally"
Expand Down Expand Up @@ -119,7 +130,34 @@
"rightHand": "hands"
}
],
"intro": "Hello. Can I help you with something? A service perhaps? Anything and everything you need I have. For a price of course. . . ",
"conversations": [
{
"condition":"none",
"player":"",
"text":"Hello. Can I help you with something? A service perhaps? Anything and everything you need I have. For a price of course. . . ",
"response":[1, 2],
"action": "no action"
},
{
"condition": "none",
"player":"How about a potion?",
"text": "Of course, I have many at my disposal. Unfortunately, you'd need to be a syndicate member to be able to buy any.",
"response":[2, 3],
"action": "no action"
},
{
"condition": "None",
"player": "Never mind then",
"text": "*bows*\n Until next time",
"action": "no action"
},
{
"condition": "None",
"player": "How do I become a syndicate member?",
"text": "Very simple actually. Just kill one of the brotherhood members for me.",
"action": "no action"
}
],
"relationships": [
{
"recruit": "enemy",
Expand Down Expand Up @@ -181,7 +219,7 @@
{
"condition": "ally",
"player": "",
"text": "I have a task for you...",
"text": "Hey, I have a task for you...",
"response": [2, 3],
"action": "no action"
},
Expand All @@ -194,16 +232,22 @@
{
"condition": "none",
"player": "What is it?",
"text": "insert quest here",
"response": [3],
"text": "There's a syndicate member hanging out up north near their headquarters. We've been needing some fresh blood around here. Kill him for me and there'll be some valuable perks in it for you",
"response": [3, 4],
"action": "no action"
},
{
"condition": "none",
"player": "I'm busy",
"text": "See you around",
"text": "Whatever, the offer always stands though",
"action": "no action"
}
},
{
"condition": "none",
"player": "Sure, why not?",
"text": "Thanks! Report back to me when you're one of us and I'll show you around",
"action": "no action"
}
]
},
"recruit": {
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/com/jadventure/game/CharacterChange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.jadventure.game;

import com.jadventure.game.entities.Player;
import com.jadventure.game.QueueProvider;

import com.google.gson.JsonObject;
import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.JsonElement;

import java.io.FileReader;
import java.io.Reader;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class CharacterChange {
public void trigger(Player player, String triggerType, String keyword) {
JsonParser parser = new JsonParser();
String fileName = "json/character_transitions.json";
try {
Reader reader = new FileReader(fileName);
JsonObject json = parser.parse(reader).getAsJsonObject();

String currentCharacter = player.getCurrentCharacterType();

JsonObject currentCharacterTransitions;
JsonObject events;
JsonObject characterEffects = new JsonObject();
boolean goAhead = false;
if (json.has(currentCharacter)) {
currentCharacterTransitions = json.get(currentCharacter).getAsJsonObject();
if (currentCharacterTransitions.has(triggerType)) {
events = currentCharacterTransitions.get(triggerType).getAsJsonObject();
if (events.has(keyword)) {
characterEffects = events.get(keyword).getAsJsonObject();
goAhead = true;
} else {
//QueueProvider.offer("Warning: The effects for the '" + triggerType + "' event and the '" + currentCharacter + "' character was not found");
}
} else {
//QueueProvider.offer("Warning: The event '" + triggerType + "' for the '" + currentCharacter + "' character was not found");
}
} else {
//QueueProvider.offer("Warning: The character '" + currentCharacter + "' was not found");
}

if (goAhead == true) {
for (Map.Entry<String, JsonElement> entry : characterEffects.entrySet()) {
String characterName = entry.getKey();
int characterLevelEffect = entry.getValue().getAsInt();
int characterLevel = player.getCharacterLevel(characterName);
int newCharacterLevel = characterLevel + characterLevelEffect;
player.setCharacterLevel(characterName, newCharacterLevel);
checkForCharacterChange(player);
}
}

} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}

public void checkForCharacterChange(Player player) {
HashMap<String, Integer> characterLevels = player.getCharacterLevels();
String currentCharacter = player.getCurrentCharacterType();
int highestCharacterLevel = player.getCharacterLevel(currentCharacter);
String highestCharacter = currentCharacter;
Iterator it = characterLevels.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
int value = (int)pairs.getValue();
if (value > highestCharacterLevel) {
highestCharacterLevel = value;
highestCharacter = (String)pairs.getKey();
}
}
if (!highestCharacter.equals(currentCharacter)) {
player.setCurrentCharacterType(highestCharacter);
QueueProvider.offer("You're character type is now changed! You are now a " + highestCharacter + "!");
}
it = characterLevels.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry)it.next();
player.setCharacterLevel((String)pairs.getKey(), (int)pairs.getValue());
}
}

}
18 changes: 18 additions & 0 deletions src/main/java/com/jadventure/game/entities/NPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* with variables not unique to the NPC, place it in the entity class.
*/
public class NPC extends Entity {
private int xpGain;
String id;

public NPC(String entityID) {
Expand All @@ -34,11 +35,28 @@ public NPC(String entityID) {
}
}
setName(json.get("name").getAsString());
setHealth(json.get("health").getAsInt());
setDamage(json.get("damage").getAsInt());
setArmour(json.get("armour").getAsInt());
setHealthMax(json.get("healthMax").getAsInt());
setLevel(json.get("level").getAsInt());
setStrength(json.get("strength").getAsInt());
setIntelligence(json.get("intelligence").getAsInt());
setDexterity(json.get("dexterity").getAsInt());
setStealth(json.get("stealth").getAsInt());
} catch (FileNotFoundException ex) {
QueueProvider.offer("Unable to open file '" + fileName + "'.");
}
}

public int getXPGain() {
return xpGain;
}

public void setXPGain(int xpGain) {
this.xpGain = xpGain;
}

public String getId() {
return id;
}
Expand Down
53 changes: 47 additions & 6 deletions src/main/java/com/jadventure/game/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,39 @@ public class Player extends Entity {
private int xp;
/** Player type */
private String type;

private static HashMap<String, Integer>characterLevels = new HashMap<String, Integer>();

public Player() {
}

protected static void setUpCharacterLevels() {
characterLevels.put("Sewer Rat", 5);
characterLevels.put("Recruit", 3);
characterLevels.put("Syndicate Member", 4);
characterLevels.put("Brotherhood Member", 4);
}

public HashMap<String, Integer> getCharacterLevels() {
return characterLevels;
}

public String getCurrentCharacterType() {
return this.type;
}

public void setCurrentCharacterType(String newCharacterType) {
this.type = newCharacterType;
}

public void setCharacterLevel(String characterType, int level) {
this.characterLevels.put(characterType, level);
}

public int getCharacterLevel(String characterType) {
int characterLevel = this.characterLevels.get(characterType);
return characterLevel;
}

protected static String getProfileFileName(String name) {
return "json/profiles/" + name + "/" + name + "_profile.json";
}
Expand Down Expand Up @@ -106,6 +135,7 @@ public static Player load(String name) {
Coordinate coordinate = new Coordinate(json.get("location").getAsString());
player.setLocation(LocationManager.getLocation(coordinate));
reader.close();
setUpCharacterLevels();
} catch (FileNotFoundException ex) {
QueueProvider.offer( "Unable to open file '" + fileName + "'.");
} catch (IOException ex) {
Expand Down Expand Up @@ -155,6 +185,7 @@ public static Player getInstance(String playerClass){
QueueProvider.offer("Not a valid class");
}
reader.close();
setUpCharacterLevels();
} catch (FileNotFoundException ex) {
QueueProvider.offer( "Unable to open file '" + fileName + "'.");
} catch (IOException ex) {
Expand Down Expand Up @@ -406,15 +437,25 @@ public LocationType getLocationType(){
}

public void attack(String opponentName) throws DeathException {
Monster opponent = null;
Monster monsterOpponent = null;
NPC npcOpponent = null;
List<Monster> monsters = getLocation().getMonsters();
List<NPC> npcs = getLocation().getNPCs();
for (int i = 0; i < monsters.size(); i++) {
if (monsters.get(i).monsterType.equalsIgnoreCase(opponentName)) {
opponent = monsters.get(i);
if (monsters.get(i).monsterType.equalsIgnoreCase(opponentName)) {
monsterOpponent = monsters.get(i);
}
}
if (opponent != null) {
new BattleMenu(opponent, this);
for (int i=0; i < npcs.size(); i++) {
if (npcs.get(i).getName().equalsIgnoreCase(opponentName)) {
npcOpponent = npcs.get(i);
}
}
if (monsterOpponent != null) {
monsterOpponent.setName(monsterOpponent.monsterType);
new BattleMenu(monsterOpponent, this);
} else if (npcOpponent != null) {
new BattleMenu(npcOpponent, this);
} else {
QueueProvider.offer("Opponent not found");
}
Expand Down
Loading