From f7747346d782672363aeedd5a0742e01a718c125 Mon Sep 17 00:00:00 2001 From: dalyjame Date: Wed, 14 Aug 2019 17:02:28 -0400 Subject: [PATCH] Added movement abilities to NPCs --- .../fmon/script/action/MessageAction.scala | 7 ++++ FakeMon/src/fmon/util/Direction.scala | 4 ++ FakeMon/src/fmon/world/Actor.scala | 24 +++++++++-- FakeMon/src/fmon/world/EventPage.scala | 10 +++-- FakeMon/src/fmon/world/GameEvent.scala | 12 ++++++ FakeMon/src/fmon/world/ui/GameView.scala | 40 +++++++++++-------- 6 files changed, 74 insertions(+), 23 deletions(-) create mode 100644 FakeMon/src/fmon/script/action/MessageAction.scala diff --git a/FakeMon/src/fmon/script/action/MessageAction.scala b/FakeMon/src/fmon/script/action/MessageAction.scala new file mode 100644 index 0000000..9b5d2cf --- /dev/null +++ b/FakeMon/src/fmon/script/action/MessageAction.scala @@ -0,0 +1,7 @@ +package fmon.script.action + +import fmon.script.Action + +class MessageAction[A](val msg: String) extends Action[A] { + override def apply(env: A) = println(msg) +} \ No newline at end of file diff --git a/FakeMon/src/fmon/util/Direction.scala b/FakeMon/src/fmon/util/Direction.scala index 6c4ebdb..d3ef3c9 100644 --- a/FakeMon/src/fmon/util/Direction.scala +++ b/FakeMon/src/fmon/util/Direction.scala @@ -1,5 +1,7 @@ package fmon.util +import fmon.Direction + object Direction extends Enumeration { case class Val protected(x: Int = 0, y: Int = 0) extends super.Val { } @@ -15,4 +17,6 @@ object Direction extends Enumeration { val Northwest = Val(x = -1, y = -1) val cardinals = Set(North, East, South, West) + + def byName(s: String): Direction = super.withName(s).asInstanceOf[Direction] } \ No newline at end of file diff --git a/FakeMon/src/fmon/world/Actor.scala b/FakeMon/src/fmon/world/Actor.scala index e44f214..6ddf91d 100644 --- a/FakeMon/src/fmon/world/Actor.scala +++ b/FakeMon/src/fmon/world/Actor.scala @@ -6,12 +6,17 @@ import fmon._ import fmon.draw._ import fmon.util.Direction -class Actor(val sprite: Sprite, var pos: Position, var move: Movement) { +abstract class Actor(val sprite: Sprite, var pos: Position, var move: Movement) { def x: Int = pos.x def y: Int = pos.y def xreal: Double = x - move.direction.x * move.completion def yreal: Double = y - move.direction.y * move.completion + val imgView = new AnimatedImageView(sprite, new Duration(600)) { + x = pos.x * Config.tileSize + y = pos.y * Config.tileSize + Config.yOffset + } + def pose: String = sprite.pose def pose_=(p: String) = { sprite.pose = p @@ -20,8 +25,21 @@ class Actor(val sprite: Sprite, var pos: Position, var move: Movement) { def slide(dt: Duration): Unit = { move.completion -= dt.toSeconds() * Config.moveSpeed if (move.completion <= 0) { - //pos += move.direction - move = new Movement(Direction.Stationary, 0) + move = selectNextMove + pos += move.direction + if (move.direction != Direction.Stationary) { + imgView.play() + } else { + imgView.stop() + } } + imgView.x = xreal * Config.tileSize + imgView.y = yreal * Config.tileSize + Config.yOffset } + + def selectNextMove: Movement +} + +class Hero(sprite: Sprite, pos: Position) extends Actor(sprite, pos, new Movement(Direction.Stationary, 0)) { + def selectNextMove = new Movement(Direction.Stationary, 0) } \ No newline at end of file diff --git a/FakeMon/src/fmon/world/EventPage.scala b/FakeMon/src/fmon/world/EventPage.scala index ee240b6..3cb9dba 100644 --- a/FakeMon/src/fmon/world/EventPage.scala +++ b/FakeMon/src/fmon/world/EventPage.scala @@ -1,13 +1,16 @@ package fmon.world -class EventPage { +import fmon.draw.Sprite +import fmon.script.Action + +class EventPage ( // Conditions // Switch // Variable // Self-Switch // Item // Actor - // Graphic + val sprite : Sprite, // Priority - Above or below // Trigger - Action button / On contact // Movement @@ -19,5 +22,6 @@ class EventPage { // Stepping Animation // Direction Fix // Pass through (no-collide) - // Contents (event effects) + val action: Action[Actor] + ) { } \ No newline at end of file diff --git a/FakeMon/src/fmon/world/GameEvent.scala b/FakeMon/src/fmon/world/GameEvent.scala index e13ec78..c26f7ee 100644 --- a/FakeMon/src/fmon/world/GameEvent.scala +++ b/FakeMon/src/fmon/world/GameEvent.scala @@ -1,6 +1,18 @@ package fmon.world +import fmon._ +import fmon.util.Direction + class GameEvent(val name: String, val pages: IndexedSeq[EventPage]) { + def currPage = pages.head // TODO : Conditions // Event Name // Event Pages +} + +class NPC(val event: GameEvent, pos: Position) + extends Actor(event.currPage.sprite, pos, new Movement(Direction.Stationary, 0.0)) { + + def selectNextMove: Movement = { + new Movement(Direction.Stationary, 0.0) + } } \ No newline at end of file diff --git a/FakeMon/src/fmon/world/ui/GameView.scala b/FakeMon/src/fmon/world/ui/GameView.scala index 6749640..0020ab1 100644 --- a/FakeMon/src/fmon/world/ui/GameView.scala +++ b/FakeMon/src/fmon/world/ui/GameView.scala @@ -27,6 +27,7 @@ import FileChooser.ExtensionFilter import fmon._ import fmon.draw._ +import fmon.script.action.MessageAction import fmon.util.Direction import fmon.world._ import fmon.util.YamlHelper @@ -39,7 +40,7 @@ class GameView { @FXML var characterPane: jfxsl.AnchorPane = _ var hero: Actor = _ - var npcs = Seq[Actor]() + var npcs = Seq[NPC]() def actors = hero +: npcs var heroImg: AnimatedImageView = _ @@ -71,22 +72,20 @@ class GameView { doodads.children ++= dtiles val heroFile = Config.homedir + raw"Progena\Resources\characters\Actor1.png" - hero = new Actor(CharSet(new File(heroFile), 1), Position(5, 4), new Movement(Direction.Stationary, 0.0)) + hero = new Hero(CharSet(new File(heroFile), 1), Position(5, 4)) - heroImg = new AnimatedImageView(hero.sprite, new Duration(600)) { - x = hero.x * Config.tileSize - y = hero.y * Config.tileSize + Config.yOffset - } + heroImg = hero.imgView - npcs = (0 until 8).map(i => - new Actor(CharSet(new File(heroFile), i), Position(7 + i, 4), new Movement(Direction.Stationary, 0.0)) - ) + npcs = (0 until 8).map(i => { + val event = new GameEvent(s"NPC $i", IndexedSeq(new EventPage( + CharSet(new File(heroFile), i), + new MessageAction(s"I am NPC $i") + ))) + new NPC(event, Position(7 + i, 3)) + }) characterPane.children += heroImg - val npcImgs = npcs.map(npc => new AnimatedImageView(npc.sprite, new Duration(600)) { - x = npc.x * Config.tileSize - y = npc.y * Config.tileSize + Config.yOffset - }) + val npcImgs = npcs.map(npc => npc.imgView) npcImgs.foreach(characterPane.children += _) timer.start() @@ -98,6 +97,7 @@ class GameView { case KeyCode.D | KeyCode.Right => setDir(East) case KeyCode.A | KeyCode.Left => setDir(West) case KeyCode.S | KeyCode.Down => setDir(South) + case KeyCode.Space => activate() case _ => () } } @@ -112,15 +112,21 @@ class GameView { } } + def activate(): Unit = { + if (hero.move.direction == Direction.Stationary) { + val dir = Direction.byName(hero.sprite.pose) + val np = hero.pos + dir + npcs.filter(npc => npc.pos == np).foreach(npc => npc.event.currPage.action(npc)) + } + } + def setDir(dir: Direction): Unit = { currDir = Some(dir) - heroImg.play() } def clearDir(dir: Direction): Unit = { if (currDir == Some(dir)) { currDir = None - heroImg.stop() } } @@ -147,12 +153,12 @@ class GameView { if (canPass && unoccupied) { hero.pos += dir hero.move = new Movement(dir, 1.0) + hero.imgView.play() } } } hero.slide(dur) - heroImg.x = hero.xreal * Config.tileSize - heroImg.y = hero.yreal * Config.tileSize + Config.yOffset + npcs.foreach(_.slide(dur)) } /*