blob: 7e441c1372b048e3837ea7813b84f963076ac891 (
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
|
package scalevalapokalypsi.Model.Entities
import scala.collection.mutable.Buffer
import scalevalapokalypsi.Model.*
/** A `Player` object represents a player character controlled by one real-life player
* of the program.
*
* A player object’s state is mutable: the player’s location and possessions can change,
* for instance.
*
* @param name the player's name
* @param initialLocation the player’s initial location
*/
class Player(name: String, initialLocation: Area) extends Entity(name, initialLocation):
private val observations: Buffer[String] = Buffer.empty
private var pendingSingEffect: Option[Float => String] = None
override def observe(observation: String): Unit =
this.observations.append(observation)
def readAndClearObservations(): Vector[String] =
val res = this.observations.toVector
observations.clear()
res
/** Returns whether this player has a pending sing effect. */
def isSinging: Boolean = this.pendingSingEffect.isDefined
/** Makes this player start singing, i.e. gives it this sing effect to
* complete.
*
* @param effect the effect to apply based on the song.
*/
def setSingEffect(effect: Float => String): Unit =
this.pendingSingEffect = Some(effect)
/** Applies the pending sing effect.
*
* @param singQuality the quality of the song
* @return a textual description of the effects of the song,
* or None if there was no pending sing effect.
*/
def applySingEffect(singQuality: Float): Option[String] =
val res = this.pendingSingEffect.map(f => f(singQuality))
this.pendingSingEffect = None
res
end Player
|