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
80
81
82
83
84
85
86
87
88
89
90
91
|
package scalevalapokalypsi.Model.Entities.NPCs
import scala.collection.mutable.Buffer
import scalevalapokalypsi.Model.*
import scalevalapokalypsi.Model.Entities.*
import scala.util.Random
/** A `NPC` object represents a non-playable in-game character controlled by
* the server using this objects `act` method. It can also be "talked to": it
* returns a dialog when asked for.
*
* A NPC object’s state is mutable: the NPC’s location and possessions can change,
* for instance.
*
* @param name the NPC's name
* @param initialLocation the NPC’s initial location
*/
abstract class NPC(
adventure: Adventure,
name: String,
initialLocation: Area,
initialHP: Int,
maxHp: Int
) extends Entity(adventure, name, initialLocation, initialHP, maxHp):
def getDialog: String
def act(): Unit
class Zombie(
adventure: Adventure,
identifier: String,
initialLocation: Area,
initialHP: Int = 20
) extends NPC(adventure, identifier, initialLocation, initialHP, 20):
private val damage = 10
private val dialogs = Vector(
"örvlg",
"grr",
"äyyrrrgrlgb ww",
"aaak brzzzwff ååö",
"äkb glan abglum",
"öub gpa"
)
override def getDialog: String =
val dialogIndex = Random.between(0, this.dialogs.length)
this.dialogs(dialogIndex)
override def act(): Unit =
val possibleVictims = this.location
.getEntities
.filter(_ != this)
.toVector
val index: Int =
if possibleVictims.isEmpty then 0
else Random.between(0, possibleVictims.length)
if possibleVictims.isEmpty then
val possibleDirections = this.location.getNeighborNames.toVector
val directionIndex = Random.between(0, possibleDirections.length*2)
possibleDirections
.toVector
.lift(directionIndex)
.flatMap(this.go(_))
.foreach(this.location.observeEvent(_))
else
this.location.observeEvent(
this.attack(possibleVictims(index))
)
private def attack(entity: Entity): Event =
if Random.nextBoolean() then
entity.takeDamage(this.damage)
Event(
Map.from(Vector((
entity,
s"${this.name} puree sinua, hyi yäk!\n" +
s"${entity.condition(0)}"
))),
s"${this.name} puree henkilöä ${entity.name}.\n" +
s"${entity.condition(1)}"
)
else
Event(
Map.from(Vector((
entity,
s"${this.name} yrittää purra sinua mutta kaatuu ohitsesi."
))),
s"${this.name} yrittää purra henkilöä ${entity.name}, mutta epäonnistuu surkeasti."
)
|