diff options
Diffstat (limited to 'src/scalevalapokalypsi/Model/Action.scala')
-rw-r--r-- | src/scalevalapokalypsi/Model/Action.scala | 65 |
1 files changed, 53 insertions, 12 deletions
diff --git a/src/scalevalapokalypsi/Model/Action.scala b/src/scalevalapokalypsi/Model/Action.scala index fdfbf75..287b008 100644 --- a/src/scalevalapokalypsi/Model/Action.scala +++ b/src/scalevalapokalypsi/Model/Action.scala @@ -1,6 +1,7 @@ package scalevalapokalypsi.Model import scalevalapokalypsi.Model.Entities.* +import scalevalapokalypsi.Model.Entities.NPCs.* /** The class `Action` represents actions that a player may take in a text * adventure game. `Action` objects are constructed on the basis of textual @@ -36,20 +37,60 @@ class Action(input: String): result.map((true, _)) case "rest" => Some((true, actor.rest())) case "get" => Some((false, actor.pickUp(this.modifiers))) - case "say" => - val to = "to" - val recipient = modifiers.reverse.takeWhile(_ != ' ').reverse - val recipientEntity = actor.location.getEntity(recipient) - val maybeTo = modifiers.slice( - modifiers.length - recipient.length - s"$to ".length, - modifiers.length - recipient.length - 1 + case "sano" => + val entityNames = actor.location.getEntityNames.map(_.toLowerCase) + val recipientNamePair = entityNames.map(name => + val possibleNamesWithSuffix = (0 to "ille".length).map(i => + modifiers.takeRight(name.length + i) + ) + possibleNamesWithSuffix.find(s => + s.take(name.length) == name + ) + .map(_.splitAt(name.length)) + ).flatten.headOption + + val recipient = recipientNamePair.flatMap(p => + actor.location.getEntity(p(0)) ) - val message = - modifiers.take(modifiers.length - recipient.length - 4) - if maybeTo == to then - recipientEntity.map(e => (false, actor.sayTo(e, message))) - else + + val message = recipientNamePair + .map(p => modifiers.dropRight(p(0).length + p(1).length)) + .filter(_.takeRight(1) == " ") + .map(_.dropRight(1)) + + message.map(m => + recipient.map(e => (false, actor.sayTo(e, m))) + ).getOrElse( Some((false, actor.say(modifiers))) + ) + case "puhu" => + val recipient = modifiers + .indices.take("ille".length + 1) + .map(i => modifiers.take(modifiers.length - i)) + .find(name => actor.location.getEntity(name).isDefined) + .flatMap(name => actor.location.getEntity(name)) + val dialog = recipient match + case Some(npc: NPC) => + s"${npc.name}: ”${npc.getDialog}”" + case Some(player: Player) => + "Et voi puhua pelaajille, vain sanoa asioita heille." + case Some(other) => + "Et voi puhua tälle olennolle." + case None => + "Kyseistä puhujaa ei löytynyt." + + val fromThirdPerson = recipient + .filter(a => a.isInstanceOf[NPC]) + .map(a => s"${actor.name} puhuu $modifiers") + + Some( + ( + false, + Event(Vector(( + actor, dialog + )).toMap, fromThirdPerson.getOrElse("")) + ) + ) case "drop" => Some((false, actor.drop(this.modifiers))) case "laula" => val end = modifiers.takeRight("suohon".length) |