aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Model/Entity.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-07 01:17:58 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-07 01:17:58 +0200
commitfdaec6534eb7ee902c75be3e4b732b6970abd859 (patch)
treef323d11dc1a33a93ee44417a58df62ef5f9ad98a /src/main/scala/Model/Entity.scala
parent12cbf4d451d1002b8872b0028acbe6bd886ca9bd (diff)
downloadscalevalapokalypsi-fdaec6534eb7ee902c75be3e4b732b6970abd859.tar.gz
scalevalapokalypsi-fdaec6534eb7ee902c75be3e4b732b6970abd859.zip
Made the server work with the adventure model
* The model was imported from the wrong version, so that needs to be fixed. * The client side doesn't work at all right now. Use netcat for testing. * There are inconveniences and bugs in the model (eg. it lists the player among entities) * Players can just see each other, not interact in any way But it's a good base.
Diffstat (limited to 'src/main/scala/Model/Entity.scala')
-rw-r--r--src/main/scala/Model/Entity.scala64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/main/scala/Model/Entity.scala b/src/main/scala/Model/Entity.scala
new file mode 100644
index 0000000..eb19606
--- /dev/null
+++ b/src/main/scala/Model/Entity.scala
@@ -0,0 +1,64 @@
+package o1game.Model
+
+import scala.collection.mutable.Map
+
+/** A `Player` object represents a player character controlled by the real-life user
+ * of the program.
+ *
+ * A player object’s state is mutable: the player’s location and possessions can change,
+ * for instance.
+ *
+ * @param startingArea the player’s initial location */
+class Entity(val name: String, initialLocation: Area):
+ private var currentLocation: Area = initialLocation
+ private var quitCommandGiven = false // one-way flag
+ private val inventory: Map[String, Item] = Map()
+
+ /** Determines if the player has indicated a desire to quit the game. */
+ def hasQuit = this.quitCommandGiven // TODO: This is probably unneccessary?
+
+ /** Returns the player’s current location. */
+ def location = this.currentLocation
+
+ /** Attempts to move the player in the given direction. This is successful if there
+ * is an exit from the player’s current location towards the direction name. Returns
+ * a description of the result: "You go DIRECTION." or "You can't go DIRECTION." */
+ def go(direction: String) =
+ val destination = this.location.neighbor(direction)
+ if destination.isDefined then
+ this.currentLocation.removeEntity(this.name)
+ this.currentLocation = destination.getOrElse(this.currentLocation)
+ destination.foreach(_.addEntity(this))
+ s"You go $direction."
+ else
+ "You can't go " + direction + "."
+
+ def pickUp(itemName: String): String =
+ this.currentLocation.removeItem(itemName) match
+ case Some(i) =>
+ this.inventory += i.name -> i
+ s"You pick up the ${i.name}"
+ case None => s"There is no $itemName here to pick up."
+
+ def drop(itemName: String): String =
+ this.inventory.remove(itemName) match
+ case Some(item) =>
+ this.currentLocation.addItem(item)
+ s"You drop the $itemName"
+ case None => "You don't have that!"
+
+ /** Causes the player to rest for a short while (this has no substantial effect in game terms).
+ * Returns a description of what happened. */
+ def rest() =
+ "You rest for a while. Better get a move on, though."
+
+ /** Signals that the player wants to quit the game. Returns a description of what happened within
+ * the game as a result (which is the empty string, in this case). */
+ def quit() =
+ this.quitCommandGiven = true
+ ""
+
+ /** Returns a brief description of the player’s state, for debugging purposes. */
+ override def toString = "Now at: " + this.location.name
+
+end Entity