Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/main/java/org/vashonsd/pirateship/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,37 @@
import org.vashonsd.pirateship.interactions.Request;
import org.vashonsd.pirateship.io.*;
import org.vashonsd.pirateship.item.*;
import org.vashonsd.pirateship.runtimeevents.listeners.PlayerDropListener;
import org.vashonsd.pirateship.runtimeevents.listeners.PlayerMovementListener;
import org.vashonsd.pirateship.runtimeevents.listeners.PlayerTakeListener;
import org.vashonsd.pirateship.structure.*;
import org.vashonsd.pirateship.minigame.*;

public class Game {
private StringRead reader;
private StringWrite writer;

private DatabaseWriter db = new DatabaseWriter();

//This is our register of current Players, each with a unique ID.
private PlayerRegistry players;

//private HashMap<String, Player> players;

private World thisWorld;

private String quitWord;

public Game(String world) throws IOException {
super();

quitWord = "exit";

//thisWorld = WorldBuilder.makeWorldByFile(world);
World thisWorld = WorldBuilder.makeWorld(world);
thisWorld = DatabaseParser.parseWorld(world);
//thisWorld = WorldBuilder.makeWorld(world);

this.players = new PlayerRegistry();
Player p = new Player("Demo", "Just a player");
p.setLocation(thisWorld.getStartingLocation());
p.addEventListeners(new PlayerMovementListener(), new PlayerTakeListener(), new PlayerDropListener());
String pid = players.EnrollPlayer(p);

reader = new UserInput();
Expand All @@ -52,6 +56,7 @@ public void Run() throws IOException {
}
writer.write(p.handle(command).getText());;
}
DatabaseWriter.worldWriter(thisWorld);
writer.write("Thanks for playing!");
}
}
67 changes: 67 additions & 0 deletions src/main/java/org/vashonsd/pirateship/interactions/Actor.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package org.vashonsd.pirateship.interactions;

import java.util.ArrayList;
import java.util.EventListener;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import org.vashonsd.pirateship.commands.Command;
import org.vashonsd.pirateship.runtimeevents.events.PlayerInventoryEvent;
import org.vashonsd.pirateship.runtimeevents.events.PlayerMovementEvent;
import org.vashonsd.pirateship.runtimeevents.listeners.*;

/**
* An Actor is anything that can interact in the World.
Expand Down Expand Up @@ -61,6 +67,43 @@ public HashMap<String, Command> getCommands() {
public void setCommands(HashMap<String, Command> commands) {
this.commands = commands;
}

/**
* Represents all the listeners that can act upon an event originating from this Actor.
*
* Once a listener is added to the Actor's listeners, that listener can handle that event
* with the "onEvent()" method.
*/
//If we end up adding more types of listeners, we will need to change these list parameters.
private List<EventListener> _listeners = new ArrayList<EventListener>();


/**
* Use this to add a listener to the current listeners on this Actor.
*/
public synchronized void addEventListener(EventListener l) {
_listeners.add(l);
}

public synchronized void addEventListeners(EventListener... l) {
for(EventListener eL: l) {
addEventListener(eL);
}
}

/**
* Use this to remove a listener from the current listeners on this Actor.
*/
public synchronized void removeEventListener(EventListener l) {
_listeners.remove(l);
}

public synchronized void removeEventListeners(EventListener... l) {
for(EventListener eL: l) {
removeEventListener(eL);
}
}


/**
* An Inventory object keeps all the Actors within this Actor.
Expand All @@ -77,10 +120,24 @@ public Inventory getInventory() {
*/
public void addToInventory(Actor a) {
a.setLocation(this);
Iterator<EventListener> i = _listeners.iterator();
while(i.hasNext()) {
try {
((PlayerTakeListener) i.next()).onPlayerTake(new PlayerInventoryEvent(this, a));
} catch(ClassCastException e) {
}
}
this.inventory.addActor(a);
}

public boolean removeFromInventory(Actor a) {
Iterator<EventListener> i = _listeners.iterator();
while(i.hasNext()) {
try {
((PlayerDropListener) i.next()).onPlayerDrop(new PlayerInventoryEvent(this, a));
} catch(ClassCastException e) {
}
}
return this.inventory.remove(a);
}

Expand Down Expand Up @@ -113,7 +170,17 @@ public Actor getLocation() {
return currentLocation;
}

/**
* This method sets the location of an Actor, but also triggers the event "PlayerMovement".
*/
public void setLocation(Actor currentLocation) {
Iterator<EventListener> i = _listeners.iterator();
while(i.hasNext()) {
try {
((PlayerMovementListener) i.next()).onPlayerMovement(new PlayerMovementEvent(this, currentLocation));
} catch(ClassCastException e) {
}
}
this.currentLocation = currentLocation;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.vashonsd.pirateship.interactions;


import org.vashonsd.pirateship.creature.*;
import org.vashonsd.pirateship.minigame.MinigameRunner;

public class ItemFactory {

private static String[] minigames = new String[] {"20Q", "Blackjack", "CC", "GF", "GuessCalc", "math", "GOPP", "Shot", "TTT"};

public static Actor newActor(String name, String typeName,
String description, String splash) {
Actor a;
if(isMinigame(typeName)) {
a = new MinigameRunner(typeName);
} else if (typeName.equalsIgnoreCase("Baguette")) {
a = new Baguette(name, description, splash);
} else if (typeName.equalsIgnoreCase("Sword")) {
a = new Sword(name, description, splash);
} else if (typeName.equalsIgnoreCase("Bandage")) {
a = new Bandage(name, description, splash);
} else if (typeName.equalsIgnoreCase("StopWatch")) {
a = new StopWatch(name);
} else if (typeName.equalsIgnoreCase("Eagle")) {
a = new Eagle(name);
} else if (typeName.equalsIgnoreCase("Kraken")) {
a = new Kraken(name, description, splash);
} else if (typeName.equalsIgnoreCase("Lizard")) {
a = new Lizard(name, description, splash);
} else if (typeName.equalsIgnoreCase("Dog")) {
a = new Dog(name, description, splash);
} else {
a = null;
}
return a;
}

private static boolean isMinigame(String typeName) {
for(String s: minigames) {
if(s.equalsIgnoreCase(typeName)) {
return true;
}
}
return false;
}
}
1 change: 1 addition & 0 deletions src/main/java/org/vashonsd/pirateship/item/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.*;


public class ItemType
{
private ArrayList<Tag> tags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static Minigame produce(String s) {
m = new PrisonEscapeFactory();
} else if (s.equalsIgnoreCase("Shot")) {
m = new ShotgunFactory();
} else if (s.equalsIgnoreCase("Tic tac toe")) {
} else if (s.equalsIgnoreCase("TTT")) {
m = new TicTacToeFactory();
}
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.vashonsd.pirateship.runtimeevents.events;

import java.util.EventObject;

import org.vashonsd.pirateship.interactions.Actor;

public class PlayerInventoryEvent extends EventObject {

private Actor recipient;

public PlayerInventoryEvent(Object source, Actor recipient) {
super(source);
this.recipient = recipient;
}

public Actor getRecipient() {
return this.recipient;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.vashonsd.pirateship.runtimeevents.events;

import java.util.EventObject;

import org.vashonsd.pirateship.interactions.Actor;

public class PlayerMovementEvent extends EventObject {

private Actor currentLocation;

public PlayerMovementEvent(Object source, Actor currentLocation) {
super(source);
this.currentLocation = currentLocation;
}

public Actor getCurrentLocation() {
return this.currentLocation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.vashonsd.pirateship.runtimeevents.listeners;

import java.io.IOException;
import java.util.EventListener;

import org.vashonsd.pirateship.interactions.Actor;
import org.vashonsd.pirateship.interactions.Player;
import org.vashonsd.pirateship.io.ConsoleOut;
import org.vashonsd.pirateship.io.StringWrite;
import org.vashonsd.pirateship.runtimeevents.events.PlayerInventoryEvent;

public class PlayerDropListener implements EventListener{

public void onPlayerDrop(PlayerInventoryEvent e){
Player p = (Player)e.getSource();
Actor a = e.getRecipient();
try {
StringWrite writer = new ConsoleOut();
writer.write(a.getName() + " has been dropped by " + p.getName());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.vashonsd.pirateship.runtimeevents.listeners;

import java.io.IOException;
import java.util.EventListener;

import org.vashonsd.pirateship.interactions.Player;
import org.vashonsd.pirateship.io.*;
import org.vashonsd.pirateship.runtimeevents.events.PlayerMovementEvent;

public class PlayerMovementListener implements EventListener {

public void onPlayerMovement(PlayerMovementEvent e) {
Player p = (Player)e.getSource();
try {
StringWrite writer = new ConsoleOut();
writer.write(p.getName() + " has entered " + e.getCurrentLocation().getName());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.vashonsd.pirateship.runtimeevents.listeners;

import java.io.IOException;
import java.util.EventListener;

import org.vashonsd.pirateship.interactions.Actor;
import org.vashonsd.pirateship.interactions.Player;
import org.vashonsd.pirateship.io.ConsoleOut;
import org.vashonsd.pirateship.io.StringWrite;
import org.vashonsd.pirateship.runtimeevents.events.PlayerInventoryEvent;

public class PlayerTakeListener implements EventListener{

public void onPlayerTake(PlayerInventoryEvent e){
Player p = (Player)e.getSource();
Actor a = e.getRecipient();
try {
StringWrite writer = new ConsoleOut();
writer.write(p.getName() + " has taken " + a.getName());
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
Loading