aboutsummaryrefslogtreecommitdiff
path: root/src/scalevalapokalypsi/Model/Entities
diff options
context:
space:
mode:
authorAleksi Heikkila <aleksi.e.heikkila@aalto.fi>2024-11-23 21:48:32 +0200
committerAleksi Heikkila <aleksi.e.heikkila@aalto.fi>2024-11-23 21:48:32 +0200
commitb11c02e8f1f4dea09847da999c2acb2f96df8a58 (patch)
tree9ded38857c98ec74a5917202afe0605ddf0310b3 /src/scalevalapokalypsi/Model/Entities
parentfe2543627bcec1ea0f7a429bede20ca293458ba9 (diff)
downloadscalevalapokalypsi-b11c02e8f1f4dea09847da999c2acb2f96df8a58.tar.gz
scalevalapokalypsi-b11c02e8f1f4dea09847da999c2acb2f96df8a58.zip
Inventory, pickup loppuun, drop loppuun
Diffstat (limited to 'src/scalevalapokalypsi/Model/Entities')
-rw-r--r--src/scalevalapokalypsi/Model/Entities/Entity.scala72
-rw-r--r--src/scalevalapokalypsi/Model/Entities/Player.scala6
2 files changed, 54 insertions, 24 deletions
diff --git a/src/scalevalapokalypsi/Model/Entities/Entity.scala b/src/scalevalapokalypsi/Model/Entities/Entity.scala
index e7cd45c..c7bc180 100644
--- a/src/scalevalapokalypsi/Model/Entities/Entity.scala
+++ b/src/scalevalapokalypsi/Model/Entities/Entity.scala
@@ -15,13 +15,14 @@ class Entity(
val name: String,
initialLocation: Area,
initialHP: Int = 100,
- val maxHP: Int = 100
+ val maxHP: Int = 100,
+ val maxInventoryWeight: Int = 10
):
private var currentLocation: Area = initialLocation
private var quitCommandGiven = false // one-way flag
- private val inventory: Map[String, Item] = Map()
private var hp = initialHP
+ private var items: Map[String, (Int, Item)] = Map()
// TODO: add logic for choosing from multiplu lines - can depend on HP etc.
/** Gets a verse to sing when attacking against this entity.
@@ -33,7 +34,7 @@ class Entity(
def takeDamage(amount: Int): Unit =
hp -= amount
if hp < 0 then
- println("Oh no, I died!")
+ println("Voi ei, kuolin!")
/** Returns a description of the physical condition of this entity,
* i.e. the damage it has taken.
@@ -86,42 +87,51 @@ class Entity(
(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."
+ s"$name saapuu tänne."
))
else None
def pickUp(itemName: String): Event =
this.currentLocation.removeItem(itemName) match
case Some(i) =>
- this.inventory += i.name -> i
- Event(
- immutable.Map.from(Vector((this, s"You pick up the ${i.name}"))),
- s"$name picks up the ${i.name}"
- )
+ val inventoryWeight = items.values.map(p => p(1).weight).sum
+ if inventoryWeight + i.weight > maxInventoryWeight then
+ Event(
+ immutable.Map.from(Vector((this, s"Voimasi eivät riitä kannattelemaan esinettä ${i.name}, koska kannat liikaa"))),
+ s"")
+ else
+ if items.contains(i.name) then
+ val (current, _) = items(i.name)
+ this.items += i.name -> (current + 1, i)
+ else
+ this.items += i.name -> (1, i)
+ Event(
+ immutable.Map.from(Vector((this, s"Poimit esineen ${i.name}"))),
+ s"$name poimi esineen ${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."
+ immutable.Map.from(Vector((this, s"Täällä ei ole esinettä $itemName noukittavaksi."))),
+ s"${this.name} yritti ottaa jotakin, mutta sai vain likaa käsilleen."
)
def drop(itemName: String): Event =
- this.inventory.remove(itemName) match
- case Some(item) =>
+ this.items.remove(itemName) match
+ case Some((current, item)) if current > 0 =>
+ if current - 1 == 0 then
+ this.items -= item.name
+ else
+ this.items += itemName -> (current - 1, item)
this.currentLocation.addItem(item)
Event(
- immutable.Map.from(Vector((this, s"You drop the $itemName"))),
- s"$name drops the $itemName"
+ immutable.Map.from(Vector((this, s"Pudotit esineen $itemName"))),
+ s"$name Pudotti esineen $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."
+ immutable.Map.from(Vector((this, "Sinulla ei ole tätä esinettä!"))),
+ s"$name yritti tonkia rpustaan esineen $itemName mutta ei löytänyt sitä."
)
def sayTo(entity: Entity, message: String): Event =
@@ -163,9 +173,25 @@ class Entity(
* @param itemName the name to check
* @return whether this entity has this item and can drop it
*/
- def canDrop(itemName: String): Boolean = this.inventory.contains(itemName)
+ def canDrop(itemName: String): Boolean = this.items.contains(itemName)
/** Returns a brief description of the player’s state, for debugging purposes. */
override def toString = s"${this.name} at ${this.location.name}"
+
+ def inventory: Event =
+ if this.items.isEmpty then
+ Event(
+ immutable.Map.from(Vector((this, s"Sinulla ei ole esineitä"))),
+ s"")
+ else
+ var inventoryList = List[String]()
+ for (name, (count, item)) <- this.items do
+ inventoryList = inventoryList :+ s"$name $count (${item.description})"
+ Event(
+ immutable.Map.from(Vector((this, s"Kannat repussasi:\n" + inventoryList.mkString("\n")))),
+ s"")
+
+
+
end Entity
diff --git a/src/scalevalapokalypsi/Model/Entities/Player.scala b/src/scalevalapokalypsi/Model/Entities/Player.scala
index cac5bf1..1dd5187 100644
--- a/src/scalevalapokalypsi/Model/Entities/Player.scala
+++ b/src/scalevalapokalypsi/Model/Entities/Player.scala
@@ -1,6 +1,6 @@
package scalevalapokalypsi.Model.Entities
-import scala.collection.mutable.Buffer
+import scala.collection.mutable.{Buffer, Map}
import scalevalapokalypsi.Model.*
/** A `Player` object represents a player character controlled by one real-life player
@@ -18,6 +18,7 @@ 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 =
@@ -71,4 +72,7 @@ class Player(name: String, initialLocation: Area) extends Entity(name, initialLo
))
event.foreach(this.location.observeEvent(_))
+
+
+
end Player