aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Model/Entities/Player.scala
blob: d6b3529270a21334a31dba3f4439bf46badbb854 (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
package scalevalapokalypsi.Model.Entities

import scala.collection.mutable.{Buffer, Map}
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 val observedEvents: Buffer[Event] = Buffer.empty
	private var pendingSingEffect: Option[SingEffect] = None

	override def observe(event: Event): Unit =
		this.observedEvents.append(event)

	def readAndClearObservations(): Vector[String] =
		val res1 = this.observations
		val res2 = this.observedEvents.map(_.descriptionFor(this))
		val res = (res1 ++ res2).toVector
		observations.clear()
		observedEvents.clear()
		res.filter(s => !(s.isEmpty))
		
	/** 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: SingEffect): Unit =
		this.pendingSingEffect = Some(effect)

	def getSingEffectTarget: Option[Entity] =
		this.pendingSingEffect.map(_.target)

	/** Applies the pending sing effect and informs the surronding Entities
	  * about the effects of the song.
	  *
	  * @param singQuality the quality of the song
	  */
	def applySingEffect(singQuality: Float): Unit =
		val res = this.pendingSingEffect.map(ef => ef(singQuality))
		this.pendingSingEffect = None
		val qualityDescriptions =
			if singQuality < .10 then
				("säälittävää", "epsilonin suuruinen")
			else if singQuality < .30 then
				("heikkoa", "vähäinen")
			else if singQuality < .60 then
				("keskinkertaista", "huomattavissa")
			else if singQuality < .80 then
				("hyvää", "huomattava")
			else ("erinomaista", "merkittävä")
		val quality =
			s"Laulu on ${qualityDescriptions(0)} ja sen vaikutus on ${qualityDescriptions(1)}."
		val event = res.map(ev => Event(
			ev.inFirstPersons.map((k, v) => (k, s"$quality\n$v")),
			s"$quality\n${ev.inThirdPerson}"
		))
		event.foreach(this.location.observeEvent(_))


				
			
end Player