Add RoomOpen command / event
This commit is contained in:
@ -17,6 +17,8 @@ import dialogue.event.OptionSelected;
|
||||
import dialogue.event.DialogueComplete;
|
||||
import dialogue.event.NextLine;
|
||||
|
||||
import dialogue.command.OpenRoomCommand;
|
||||
|
||||
@:uiComp("game-container")
|
||||
class GameContainer extends h2d.Flow implements h2d.domkit.Object {
|
||||
static var SRC = <game-container>
|
||||
@ -95,6 +97,7 @@ class Game extends hxd.App {
|
||||
globalEventBus.subscribe(OptionsShown, renderOptions);
|
||||
globalEventBus.subscribe(OptionSelected, onOptionSelected);
|
||||
globalEventBus.subscribe(DialogueComplete, onDialogueComplete);
|
||||
dialogueManager.addCommandHandler(new OpenRoomCommand(globalEventBus));
|
||||
|
||||
#if debug
|
||||
setGameScene(new WorldMapScene(root));
|
||||
|
@ -12,6 +12,7 @@ import hxyarn.compiler.Compiler;
|
||||
import hxyarn.compiler.CompilationJob;
|
||||
|
||||
import event.EventBus;
|
||||
import dialogue.command.ICommandHandler;
|
||||
import dialogue.event.LineShown;
|
||||
import dialogue.event.OptionsShown;
|
||||
import dialogue.event.OptionsShown.OptionChoice;
|
||||
@ -26,6 +27,7 @@ class DialogueManager {
|
||||
var storage = new MemoryVariableStore();
|
||||
var dialogue:Dialogue;
|
||||
var stringTable:Map<String, StringInfo>;
|
||||
var commandHandlers = new Map<String, ICommandHandler>();
|
||||
|
||||
public var waitingForOption:Bool = false;
|
||||
|
||||
@ -118,6 +120,25 @@ class DialogueManager {
|
||||
}
|
||||
|
||||
public function commandHandler(command:Command) {
|
||||
var parts = command.text.split(' ');
|
||||
var key = parts.shift();
|
||||
if (commandHandlers.exists(key)) {
|
||||
if (commandHandlers.get(key).handleCommand(parts)) {
|
||||
resume();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
resume();
|
||||
}
|
||||
|
||||
public function addCommandHandler(handler:ICommandHandler) {
|
||||
if (handler.commandName == null || handler.commandName == "") {
|
||||
trace("Command handler has no command name");
|
||||
return;
|
||||
}
|
||||
|
||||
commandHandlers.set(handler.commandName, handler);
|
||||
}
|
||||
|
||||
public function nodeCompleteHandler(nodeName:String) {}
|
||||
|
6
src/dialogue/command/ICommandHandler.hx
Normal file
6
src/dialogue/command/ICommandHandler.hx
Normal file
@ -0,0 +1,6 @@
|
||||
package dialogue.command;
|
||||
|
||||
interface ICommandHandler {
|
||||
var commandName:String;
|
||||
function handleCommand(args:Array<String>):Bool;
|
||||
}
|
19
src/dialogue/command/OpenRoomCommand.hx
Normal file
19
src/dialogue/command/OpenRoomCommand.hx
Normal file
@ -0,0 +1,19 @@
|
||||
package dialogue.command;
|
||||
|
||||
import event.EventBus;
|
||||
import event.OpenRoomEvent;
|
||||
|
||||
class OpenRoomCommand implements ICommandHandler {
|
||||
public var commandName:String = "room";
|
||||
|
||||
var eventBus:EventBus;
|
||||
|
||||
public function new(eventBus:EventBus) {
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
public function handleCommand(args:Array<String>):Bool {
|
||||
eventBus.publishEvent(new OpenRoomEvent(args[0]));
|
||||
return true;
|
||||
}
|
||||
}
|
9
src/event/OpenRoomEvent.hx
Normal file
9
src/event/OpenRoomEvent.hx
Normal file
@ -0,0 +1,9 @@
|
||||
package event;
|
||||
|
||||
class OpenRoomEvent implements IEvent {
|
||||
public var roomName:String;
|
||||
|
||||
public function new(rn:String) {
|
||||
this.roomName = rn;
|
||||
}
|
||||
}
|
@ -1,13 +1,22 @@
|
||||
package scene;
|
||||
|
||||
import dialogue.event.StartDialogueNode;
|
||||
import event.OpenRoomEvent;
|
||||
import TilemapLayer;
|
||||
|
||||
typedef Room = {
|
||||
rect:h2d.Graphics,
|
||||
area:h2d.Interactive,
|
||||
isOpen:Bool
|
||||
}
|
||||
|
||||
@:uiComp("world-map-scene")
|
||||
class WorldMapScene extends h2d.Flow implements GameScene implements h2d.domkit.Object {
|
||||
public var width:Int;
|
||||
public var height:Int;
|
||||
|
||||
var rooms:Map<String,Room> = new Map<String,Room>();
|
||||
|
||||
public function new(?parent) {
|
||||
super(parent);
|
||||
initComponent();
|
||||
@ -16,6 +25,8 @@ class WorldMapScene extends h2d.Flow implements GameScene implements h2d.domkit.
|
||||
|
||||
var map = Game.loader.loadTMX("map.tmx");
|
||||
|
||||
Game.current.globalEventBus.subscribe(OpenRoomEvent, openRoom);
|
||||
|
||||
for (layer in (map.layers)) {
|
||||
switch(layer.kind) {
|
||||
case TTileLayer:
|
||||
@ -32,8 +43,14 @@ class WorldMapScene extends h2d.Flow implements GameScene implements h2d.domkit.
|
||||
area.y = obj.y;
|
||||
area.onClick = function(_) {
|
||||
trace("Room:" + obj.name);
|
||||
Game.current.globalEventBus.publishEvent(new StartDialogueNode("Room:" + obj.name));
|
||||
var room = rooms.get(obj.name);
|
||||
if (room.isOpen) {
|
||||
Game.current.globalEventBus.publishEvent(new StartDialogueNode("Room:" + obj.name));
|
||||
} else {
|
||||
Game.current.globalEventBus.publishEvent(new StartDialogueNode("Room not open"));
|
||||
}
|
||||
};
|
||||
rooms.set(obj.name, {rect: rect, area: area, isOpen: false});
|
||||
}
|
||||
case TImageLayer:
|
||||
case TGroup:
|
||||
@ -42,10 +59,17 @@ class WorldMapScene extends h2d.Flow implements GameScene implements h2d.domkit.
|
||||
|
||||
#if debug
|
||||
trace('skipping intro dialogue');
|
||||
Game.current.globalEventBus.publishEvent(new OpenRoomEvent("hangar"));
|
||||
#else
|
||||
Game.current.globalEventBus.publishEvent(new StartDialogueNode("Intro"));
|
||||
#end
|
||||
}
|
||||
|
||||
public function openRoom(event:OpenRoomEvent) {
|
||||
var room = rooms.get(event.roomName);
|
||||
room.isOpen = true;
|
||||
room.area.alpha = 0;
|
||||
}
|
||||
|
||||
public function getH2dObject() {
|
||||
return this;
|
||||
|
Reference in New Issue
Block a user