aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Model/Action.scala
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-17 02:38:55 +0200
committerJoel Kronqvist <joel.kronqvist@iki.fi>2024-11-17 02:38:55 +0200
commit8595e892abc0e0554f589ed2eb88c351a347fbd4 (patch)
treed86a85243be6719f30646094e7a86e5d3d87aa68 /src/main/scala/Model/Action.scala
parente0e720c1b78506f1f9c00e2d275caa170becc927 (diff)
downloadscalevalapokalypsi-8595e892abc0e0554f589ed2eb88c351a347fbd4.tar.gz
scalevalapokalypsi-8595e892abc0e0554f589ed2eb88c351a347fbd4.zip
Implemented talking
Diffstat (limited to 'src/main/scala/Model/Action.scala')
-rw-r--r--src/main/scala/Model/Action.scala19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/main/scala/Model/Action.scala b/src/main/scala/Model/Action.scala
index 9f81256..55f7f27 100644
--- a/src/main/scala/Model/Action.scala
+++ b/src/main/scala/Model/Action.scala
@@ -10,24 +10,39 @@ class Action(input: String):
private val verb = commandText.takeWhile( _ != ' ' )
private val modifiers = commandText.drop(verb.length).trim
- def takesATurnFor(actor: Entity): Boolean =
+ def takesATurnFor(actor: Player): 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 "say" => false
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`
* wrapper; if the command was not recognized, `None` is returned. */
- def execute(actor: Entity): Option[String] =
+ def execute(actor: Player): Option[String] =
val oldLocation = actor.location
val resOption: Option[(String, String)] = this.verb match
case "go" => Some(actor.go(this.modifiers))
case "rest" => Some(actor.rest())
case "get" => Some(actor.pickUp(this.modifiers))
+ case "say" =>
+ val to = "to"
+ val recipient = modifiers.reverse.takeWhile(_ != ' ').reverse
+ val recipientEntity = actor.location.getEntity(recipient)
+ val maybeTo = modifiers.slice(
+ modifiers.length - recipient.length - s"$to ".length,
+ modifiers.length - recipient.length - 1
+ )
+ val message =
+ modifiers.take(modifiers.length - recipient.length - 4)
+ if maybeTo == to then
+ recipientEntity.map(actor.sayTo(_, message))
+ else
+ Some(actor.say(modifiers))
case "drop" => Some(actor.drop(this.modifiers))
case "xyzzy" => Some((
"The grue tastes yummy.",