diff options
author | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-07 22:52:25 +0200 |
---|---|---|
committer | Joel Kronqvist <joel.kronqvist@iki.fi> | 2024-11-07 22:52:25 +0200 |
commit | 239571e3408a3187953bef1dd5d516461bad0e31 (patch) | |
tree | 67c062a635affc90c08a92ac61adfed2445985d8 /src/main/scala/Model | |
parent | def8975617e5c6da431e41cc889d167b0f2e2bb0 (diff) | |
download | scalevalapokalypsi-239571e3408a3187953bef1dd5d516461bad0e31.tar.gz scalevalapokalypsi-239571e3408a3187953bef1dd5d516461bad0e31.zip |
Added turns, time limits and turn order randomization (feature/bug?)
Diffstat (limited to 'src/main/scala/Model')
-rw-r--r-- | src/main/scala/Model/Action.scala | 8 | ||||
-rw-r--r-- | src/main/scala/Model/Area.scala | 14 | ||||
-rw-r--r-- | src/main/scala/Model/Entity.scala | 8 | ||||
-rw-r--r-- | src/main/scala/Model/Item.scala | 2 |
4 files changed, 31 insertions, 1 deletions
diff --git a/src/main/scala/Model/Action.scala b/src/main/scala/Model/Action.scala index 84704ce..11b0bc8 100644 --- a/src/main/scala/Model/Action.scala +++ b/src/main/scala/Model/Action.scala @@ -10,6 +10,14 @@ class Action(input: String): private val verb = commandText.takeWhile( _ != ' ' ) private val modifiers = commandText.drop(verb.length).trim + def takesATurnFor(actor: Entity): Boolean = + this.verb match + case "rest" => true + case "go" => actor.location.hasNeighbor(modifiers) + case "get" => actor.location.hasItem(this.modifiers) + case "drop" => actor.canDrop(this.modifiers) + case other => false + /** Causes the given player to take the action represented by this object, assuming * that the command was understood. Returns a description of what happened as a result * of the action (such as “You go west.”). The description is returned in an `Option` diff --git a/src/main/scala/Model/Area.scala b/src/main/scala/Model/Area.scala index 97c75bf..f5b5289 100644 --- a/src/main/scala/Model/Area.scala +++ b/src/main/scala/Model/Area.scala @@ -17,7 +17,16 @@ class Area(val name: String, var description: String): /** Returns the area that can be reached from this area by moving in the given direction. The result * is returned in an `Option`; `None` is returned if there is no exit in the given direction. */ - def neighbor(direction: String) = this.neighbors.get(direction) + def neighbor(direction: String): Option[Area] = + this.neighbors.get(direction) + + /** Tells whether this area has a neighbor in the given direction. + * + * @param direction the direction to check + * @return whether there is a neighbor in the direction + */ + def hasNeighbor(direction: String): Boolean = + this.neighbors.contains(direction) /** Adds an exit from this area to the given area. The neighboring area is reached by moving in * the specified direction from this area. */ @@ -44,6 +53,9 @@ class Area(val name: String, var description: String): def addItems(items: IterableOnce[Item]) = items.iterator.foreach(i => this.items += i.name -> i) + def hasItem(itemName: String) = this.items.contains(itemName) + + /** Removes the specified item if it exists. * * @param itemName the name of the item to remove diff --git a/src/main/scala/Model/Entity.scala b/src/main/scala/Model/Entity.scala index eb19606..c18ffea 100644 --- a/src/main/scala/Model/Entity.scala +++ b/src/main/scala/Model/Entity.scala @@ -47,6 +47,14 @@ class Entity(val name: String, initialLocation: Area): s"You drop the $itemName" case None => "You don't have that!" + /** Tells whether this entity can drop the specified item + * (if an action were to specify so). + * + * @param itemName the name to check + * @return whether this entity has this item and can drop it + */ + def canDrop(itemName: String): Boolean = this.inventory.contains(itemName) + /** 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() = diff --git a/src/main/scala/Model/Item.scala b/src/main/scala/Model/Item.scala index 3809561..229828d 100644 --- a/src/main/scala/Model/Item.scala +++ b/src/main/scala/Model/Item.scala @@ -1,5 +1,7 @@ package o1game.Model +import scala.annotation.targetName + /** The class `Item` represents items in a text adventure game. Each item has a name * and a longer description. (In later versions of the adventure game, items may * have other features as well.) |