diff options
Diffstat (limited to 'src/scalevalapokalypsi/Model/Entities/Entity.scala')
-rw-r--r-- | src/scalevalapokalypsi/Model/Entities/Entity.scala | 94 |
1 files changed, 68 insertions, 26 deletions
diff --git a/src/scalevalapokalypsi/Model/Entities/Entity.scala b/src/scalevalapokalypsi/Model/Entities/Entity.scala index 26dd7dc..e7cd45c 100644 --- a/src/scalevalapokalypsi/Model/Entities/Entity.scala +++ b/src/scalevalapokalypsi/Model/Entities/Entity.scala @@ -1,8 +1,10 @@ package scalevalapokalypsi.Model.Entities -import scala.collection.mutable.{Buffer,Map} +import scala.collection.mutable.{Buffer, Map} import scalevalapokalypsi.Model.* +import scala.collection.immutable + /** An in-game entity. * @@ -69,46 +71,91 @@ class Entity( * direction name. Returns a description of the result: "You go DIRECTION." * or "You can't go DIRECTION." */ - def go(direction: String): (String, String) = + def go(direction: String): Option[Event] = val destination = this.location.neighbor(direction) + val oldEntities = this.location.getEntities.filter(_ != this) + val newEntities = destination.map(_.getEntities) if destination.isDefined then val removeSuccess = this.currentLocation.removeEntity(this.name) assert(removeSuccess.isDefined) // Production - assertions off this.currentLocation = destination.getOrElse(this.currentLocation) destination.foreach(_.addEntity(this)) - (s"You go $direction.", s"$name goes $direction") - else - ( - s"You can't go $direction.", - s"$name tries to go $direction and stumbles in their feet." - ) - def pickUp(itemName: String): (String, String) = + val leaving = oldEntities.zip( + Vector.fill + (oldEntities.size) + (s"${this.name} leaves this location.") + ) + //val arriving = newEntities.map(n => n.zip( + // Vector.fill + // (n.size) + // (s"${this.name} arrives here.") + //)).getOrElse(Vector()) + val self = Vector((this, s"You go $direction.")) + Some(Event( + (leaving ++ self).toMap, + s"$name arrives here." + )) + else None + + def pickUp(itemName: String): Event = this.currentLocation.removeItem(itemName) match case Some(i) => this.inventory += i.name -> i - (s"You pick up the ${i.name}", s"$name picks up the ${i.name}") - case None => ( - s"There is no $itemName here to pick up.", + Event( + immutable.Map.from(Vector((this, s"You pick up the ${i.name}"))), + s"$name picks up the ${i.name}" + ) + case None => Event( + immutable.Map.from(Vector((this, s"There is no $itemName here to pick up."))), s"${this.name} tries to pick up something but gets just dirt in their hands." ) - def drop(itemName: String): (String, String) = + def drop(itemName: String): Event = this.inventory.remove(itemName) match case Some(item) => this.currentLocation.addItem(item) - (s"You drop the $itemName", s"$name drops the $itemName") - case None => ( - "You don't have that!", + Event( + immutable.Map.from(Vector((this, s"You drop the $itemName"))), + s"$name drops the $itemName" + ) + case None => Event( + immutable.Map.from(Vector((this, "You don't have that!"))), s"$name reaches their backpack to drop $itemName but miserably fails to find it there." ) - def sayTo(entity: Entity, message: String): (String, String) = - entity.observeString(s"${this.name}: \"$message\"") - (s"You say so to ${entity.name}.", "") + def sayTo(entity: Entity, message: String): Event = + if entity == this then this.ponder(message: String) + else + Event( + immutable.Map.from(Vector( + (this, s"Sanot niin henkilölle ${entity.name}."), + (entity, s"${this.name}: “${message}”") + )), + s"Kuulet henkilön ${this.name} sanovan jotain henkilölle ${entity.name}" + ) - def say(message: String): (String, String) = - ("You say that aloud.", s"$name: \"$message\"") + def ponder(message: String): Event = + Event( + immutable.Map.from(Vector( + (this, s"Mietit itseksesi: “$message”") + )), + s"${this.name} näyttää pohtivan jotain itsekseen." + ) + + def say(message: String): Event = + Event( + immutable.Map.from(Vector((this, "Sanot niin ääneen."))), + s"$name: “$message”" + ) + + /** Causes the player to rest for a turn. + * Returns a description of what happened. */ + def rest(): Event = + Event( + immutable.Map.from(Vector((this, "Lepäät hetken."))), + s"${this.name} levähtää." + ) /** Tells whether this entity can drop the specified item * (if an action were to specify so). @@ -118,11 +165,6 @@ class Entity( */ def canDrop(itemName: String): Boolean = this.inventory.contains(itemName) - /** Causes the player to rest for a turn. - * Returns a description of what happened. */ - def rest(): (String, String) = - ("You rest for a while. Better get a move on, though.", "") - /** Returns a brief description of the player’s state, for debugging purposes. */ override def toString = s"${this.name} at ${this.location.name}" |