aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Model/Adventure.scala
blob: 2bf6cfec5c795e8d3e27717c79c053debc6a400c (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package scalevalapokalypsi.Model

import scala.collection.mutable.Map

/** The class `Adventure` represents text adventure games. An adventure consists of a player and
  * a number of areas that make up the game world. It provides methods for playing the game one
  * turn at a time and for checking the state of the game.
  *
  * N.B. This version of the class has a lot of “hard-coded” information that pertains to a very
  * specific adventure game that involves a small trip through a twisted forest. All newly created
  * instances of class `Adventure` are identical to each other. To create other kinds of adventure
  * games, you will need to modify or replace the source code of this class. */
class Adventure(val playerNames: Vector[String]):

	private val middle = Area("Forest", "You are somewhere in the forest. There are a lot of trees here.\nBirds are singing.")
	private val northForest = Area("Forest", "You are somewhere in the forest. A tangle of bushes blocks further passage north.\nBirds are singing.")
	private val southForest = Area("Forest", "The forest just goes on and on.")
	private val clearing = Area("Forest Clearing", "You are at a small clearing in the middle of forest.\nNearly invisible, twisted paths lead in many directions.")
	private val tangle = Area("Tangle of Bushes", "You are in a dense tangle of bushes. It's hard to see exactly where you're going.")
	private val home = Area("Home", "Home sweet home! Now the only thing you need is a working remote control.")
	private val destination = home

	middle.setNeighbors(Vector("north" -> northForest, "east" -> tangle, "south" -> southForest, "west" -> clearing))
	northForest.setNeighbors(Vector("east" -> tangle, "south" -> middle, "west" -> clearing))
	southForest.setNeighbors(Vector("north" -> middle, "east" -> tangle, "south" -> southForest, "west" -> clearing))
	clearing.setNeighbors(Vector("north" -> northForest, "east" -> middle, "south" -> southForest, "west" -> northForest))
	tangle.setNeighbors(Vector("north" -> northForest, "east" -> home,	 "south" -> southForest, "west" -> northForest))
	home.setNeighbors(Vector("west" -> tangle))

	clearing.addItem(Item("battery", "It's a small battery cell. Looks new."))
	southForest.addItem(Item(
		"remote",
		"It's the remote control for your TV.\n" +
		"What it was doing in the forest, you have no idea.\n" +
		"Problem is, there's no battery."
	))

	val players: Map[String, Player] = Map()
	playerNames.foreach(this.addPlayer(_))

	val entities: Map[String, Entity] = Map()
	private val gruu = Entity("Gruu", northForest)
	northForest.addEntity(gruu)
	this.entities += gruu.name -> gruu

	/** Adds a player entity with the specified name to the game.
	  *
	  * @param name the name of the player entity to add
	  * @return the created player entity
	  */
	def addPlayer(name: String): Player =
		val newPlayer = Player(name, middle)
		middle.addEntity(newPlayer)
		players += name -> newPlayer
		newPlayer

	/** Gets the player entity with the specified name.
	 *
	 * @param name name of the player to find
	 * @return the player, if one with the name was found
	 */
	def getPlayer(name: String): Option[Player] = this.players.get(name)

	def getEntity[A >: Entity](name: String) =
		this.players.getOrElse(name, this.entities.get(name))

	/** Returns a message that is to be displayed to the player at the beginning of the game. */
	def welcomeMessage = "Generic welcome message"

	/** Plays a turn by executing the given in-game command, such as “go west”. Returns a textual
	  * report of what happened, or an error message if the command was unknown. In the latter
	  * case, no turns elapse. */
	def playTurnOfPlayer(playerName: String, command: String): Option[String] =
		val action = Action(command)
		val actor = this.players.get(playerName)
		actor.flatMap(action.execute(_))

end Adventure