aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Model/Entities
diff options
context:
space:
mode:
Diffstat (limited to 'src/scalevalapokalypsi/Model/Entities')
-rw-r--r--src/scalevalapokalypsi/Model/Entities/Entity.scala6
-rw-r--r--src/scalevalapokalypsi/Model/Entities/NPCs/NPC.scala89
-rw-r--r--src/scalevalapokalypsi/Model/Entities/Player.scala5
3 files changed, 92 insertions, 8 deletions
diff --git a/src/scalevalapokalypsi/Model/Entities/Entity.scala b/src/scalevalapokalypsi/Model/Entities/Entity.scala
index c7bc180..336a6b1 100644
--- a/src/scalevalapokalypsi/Model/Entities/Entity.scala
+++ b/src/scalevalapokalypsi/Model/Entities/Entity.scala
@@ -59,10 +59,7 @@ class Entity(
("Olet täysin kunnossa.", s"$name näyttää kuin vastasyntyneeltä.")
/** Does nothing, except possibly in inherited classes. */
- def observeString(observation: String): Unit =
- println(" [debug] entity got observation string & discarded it")
- def observe(event: Event): Unit =
- println(" [debug] entity got observation event & discarded it")
+ def observe(event: Event): Unit = ()
/** Returns the player’s current location. */
def location = this.currentLocation
@@ -194,4 +191,5 @@ class Entity(
+
end Entity
diff --git a/src/scalevalapokalypsi/Model/Entities/NPCs/NPC.scala b/src/scalevalapokalypsi/Model/Entities/NPCs/NPC.scala
new file mode 100644
index 0000000..21709ba
--- /dev/null
+++ b/src/scalevalapokalypsi/Model/Entities/NPCs/NPC.scala
@@ -0,0 +1,89 @@
+
+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(
+ name: String,
+ initialLocation: Area,
+ initialHP: Int,
+ maxHp: Int
+) extends Entity(name, initialLocation, initialHP, maxHp):
+ def getDialog: String
+ def act(): Unit
+
+class Zombie(
+ identifier: String,
+ initialLocation: Area,
+ initialHP: Int = 20
+) extends NPC(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(_))
+ .map(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."
+ )
diff --git a/src/scalevalapokalypsi/Model/Entities/Player.scala b/src/scalevalapokalypsi/Model/Entities/Player.scala
index 1dd5187..d6b3529 100644
--- a/src/scalevalapokalypsi/Model/Entities/Player.scala
+++ b/src/scalevalapokalypsi/Model/Entities/Player.scala
@@ -18,9 +18,6 @@ class Player(name: String, initialLocation: Area) extends Entity(name, initialLo
private val observedEvents: Buffer[Event] = Buffer.empty
private var pendingSingEffect: Option[SingEffect] = None
-
- override def observeString(observation: String): Unit =
- this.observations.append(observation)
override def observe(event: Event): Unit =
this.observedEvents.append(event)
@@ -30,7 +27,7 @@ class Player(name: String, initialLocation: Area) extends Entity(name, initialLo
val res = (res1 ++ res2).toVector
observations.clear()
observedEvents.clear()
- res
+ res.filter(s => !(s.isEmpty))
/** Returns whether this player has a pending sing effect. */
def isSinging: Boolean = this.pendingSingEffect.isDefined