aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/Model/Action.scala
blob: 84704ce9dc51f0bd78e5c5a2452a26197f145606 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package o1game.Model

/** The class `Action` represents actions that a player may take in a text adventure game.
  * `Action` objects are constructed on the basis of textual commands and are, in effect,
  * parsers for such commands. An action object is immutable after creation.
  * @param input	a textual in-game command such as “go east” or “rest” */
class Action(input: String):

	private val commandText = input.trim.toLowerCase
	private val verb        = commandText.takeWhile( _ != ' ' )
	private val modifiers   = commandText.drop(verb.length).trim

	/** 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] =
		this.verb match
			case "go"    => Some(actor.go(this.modifiers))
			case "rest"  => Some(actor.rest())
			case "get"   => Some(actor.pickUp(this.modifiers))
			case "drop"  => Some(actor.drop(this.modifiers))
			case "xyzzy" => Some("The grue tastes yummy.")
			case "quit"  => Some(actor.quit())
			case other   => None

	/** Returns a textual description of the action object, for debugging purposes. */
	override def toString = s"$verb (modifiers: $modifiers)"

end Action