From fe2543627bcec1ea0f7a429bede20ca293458ba9 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Mon, 18 Nov 2024 02:01:12 +0200 Subject: Major change! Changed Events to have multiple first persons & changed sending action results to clients using this. This improves code and made it easy to finally make arrival/leaving messages when going from one area to another! This commit is too big indeed, and there are probably bugs. I'm annoyed we didn't set up unit testing during the course. I should've taken my time to set up IDEA myself for that. Now the bugs will just have to be fixed in future commits. --- src/scalevalapokalypsi/Model/Entities/Entity.scala | 94 ++++++++++++++++------ 1 file changed, 68 insertions(+), 26 deletions(-) (limited to 'src/scalevalapokalypsi/Model/Entities/Entity.scala') 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}" -- cgit v1.2.3