diff options
-rw-r--r-- | src/scalevalapokalypsi/Model/Action.scala | 1 | ||||
-rw-r--r-- | src/scalevalapokalypsi/Model/Adventure.scala | 8 | ||||
-rw-r--r-- | src/scalevalapokalypsi/Model/Entities/Entity.scala | 73 | ||||
-rw-r--r-- | src/scalevalapokalypsi/Model/Entities/Player.scala | 5 | ||||
-rw-r--r-- | src/scalevalapokalypsi/Model/Item.scala | 4 |
5 files changed, 62 insertions, 29 deletions
diff --git a/src/scalevalapokalypsi/Model/Action.scala b/src/scalevalapokalypsi/Model/Action.scala index 287b008..c7c8a65 100644 --- a/src/scalevalapokalypsi/Model/Action.scala +++ b/src/scalevalapokalypsi/Model/Action.scala @@ -37,6 +37,7 @@ class Action(input: String): result.map((true, _)) case "rest" => Some((true, actor.rest())) case "get" => Some((false, actor.pickUp(this.modifiers))) + case "inventory" => Some((false, actor.inventory)) case "sano" => val entityNames = actor.location.getEntityNames.map(_.toLowerCase) val recipientNamePair = entityNames.map(name => diff --git a/src/scalevalapokalypsi/Model/Adventure.scala b/src/scalevalapokalypsi/Model/Adventure.scala index 09eed54..ba45abe 100644 --- a/src/scalevalapokalypsi/Model/Adventure.scala +++ b/src/scalevalapokalypsi/Model/Adventure.scala @@ -15,8 +15,8 @@ import scalevalapokalypsi.Model.Entities.NPCs.* */ class Adventure(val playerNames: Vector[String]): - private val middle = Area("Forest", "You are somewhere in the forest. There are a lot of trees here.\nBirds are singing.") - private val northForest = Area("Forest", "You are somewhere in the forest. A tangle of bushes blocks further passage north.\nBirds are singing.") + private val middle = Area("Forest", "Olet keskellä metsää. Metsä on täynnä puita.\nLintua laulaa.") + private val northForest = Area("Forest", "Olet keskellä metsää. Tiheä pensaikko estää sinua kulkemasta pohjoiseen. \nLintua laulaa.") private val southForest = Area("Forest", "The forest just goes on and on.") private val clearing = Area("Forest Clearing", "You are at a small clearing in the middle of forest.\nNearly invisible, twisted paths lead in many directions.") private val tangle = Area("Tangle of Bushes", "You are in a dense tangle of bushes. It's hard to see exactly where you're going.") @@ -29,11 +29,11 @@ class Adventure(val playerNames: Vector[String]): tangle.setNeighbors(Vector("north" -> northForest, "east" -> home, "south" -> southForest, "west" -> northForest)) home.setNeighbors(Vector("west" -> tangle)) - clearing.addItem(Item("battery", "It's a small battery cell. Looks new.")) + clearing.addItem(Item("battery", "It's a small battery cell. Looks new.", 1)) southForest.addItem(Item( "laulukäärö", "Jukranpujut, löysit laulukäärön!\n" + - "Et vielä voi tehdä sillä mitään, koska et edes osaa laula." + "Et vielä voi tehdä sillä mitään, koska et edes osaa laula.", 1 )) val entities: Map[String, Entity] = Map() diff --git a/src/scalevalapokalypsi/Model/Entities/Entity.scala b/src/scalevalapokalypsi/Model/Entities/Entity.scala index 0427297..336a6b1 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. @@ -83,42 +84,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 = @@ -160,9 +170,26 @@ 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 7d166b4..d6b3529 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 @@ -69,4 +69,7 @@ class Player(name: String, initialLocation: Area) extends Entity(name, initialLo )) event.foreach(this.location.observeEvent(_)) + + + end Player diff --git a/src/scalevalapokalypsi/Model/Item.scala b/src/scalevalapokalypsi/Model/Item.scala index c1ebaa1..cccb489 100644 --- a/src/scalevalapokalypsi/Model/Item.scala +++ b/src/scalevalapokalypsi/Model/Item.scala @@ -9,10 +9,12 @@ import scala.annotation.targetName * @param name the item’s name * @param description the item’s description */ -class Item(val name: String, val description: String): +class Item(val name: String, val description: String, val weight: Int): /** Returns a short textual representation of the item (its name, that is). */ override def toString = this.name + + end Item |