[common] add GameDispatcher
This commit is contained in:
@@ -31,7 +31,7 @@ import ru.m.tankz.preset.DotaGame;
|
||||
var tankConfig = resultState.config.getTank(tankType);
|
||||
view.tank = tankConfig == null ? 'ba' : tankConfig.skin;
|
||||
view.color = resultState.config.getColor(player.id);
|
||||
view.live = player.frags;
|
||||
view.life = player.frags;
|
||||
view.score = player.score;
|
||||
return view;
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@ package ru.m.tankz.view.classic;
|
||||
|
||||
import haxework.view.LabelView;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.preset.ClassicGame;
|
||||
import ru.m.tankz.Type.PlayerId;
|
||||
import ru.m.tankz.view.common.IGamePanel;
|
||||
import ru.m.tankz.view.common.LifeView;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.preset.ClassicGame;
|
||||
|
||||
@:template class ClassicGamePanel extends VGroupView implements IGamePanel {
|
||||
|
||||
@@ -14,26 +15,27 @@ import ru.m.tankz.preset.ClassicGame;
|
||||
@:view var player2:LifeView;
|
||||
@:view var level:LabelView;
|
||||
|
||||
public var game:Game;
|
||||
private var player1Id:PlayerId = new PlayerId(ClassicGame.HUMAN, 0);
|
||||
private var player2Id:PlayerId = new PlayerId(ClassicGame.HUMAN, 1);
|
||||
|
||||
private function updateViews():Void {
|
||||
level.text = 'Level ${game.state.level}';
|
||||
bot.live = game.teams[ClassicGame.BOT].life;
|
||||
player1.live = game.teams[ClassicGame.HUMAN].players[0].state.life;
|
||||
player1.score = game.teams[ClassicGame.HUMAN].players[0].state.score;
|
||||
if (game.teams[ClassicGame.HUMAN].players[1] != null) {
|
||||
public function onGameStart(state:GameState):Void {
|
||||
level.text = 'Level ${state.level}';
|
||||
}
|
||||
|
||||
public function onGameChange(state:GameState):Void {
|
||||
bot.life = state.getTeamLife(ClassicGame.BOT);
|
||||
player1.life = state.getPlayerLife(player1Id);
|
||||
player1.score = state.getPlayerScore(player1Id);
|
||||
if (true) {
|
||||
player2.visible = true;
|
||||
player2.live = game.teams[ClassicGame.HUMAN].players[1].state.life;
|
||||
player2.score = game.teams[ClassicGame.HUMAN].players[1].state.score;
|
||||
player2.life = state.getPlayerLife(player2Id);
|
||||
player2.score = state.getPlayerScore(player2Id);
|
||||
} else {
|
||||
player2.visible = false;
|
||||
}
|
||||
}
|
||||
|
||||
override public function update():Void {
|
||||
if (game != null) {
|
||||
updateViews();
|
||||
}
|
||||
super.update();
|
||||
public function onGameComplete(state:GameState):Void {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,12 +44,13 @@ class GameFrame extends GroupView {
|
||||
game = new Game(state.type);
|
||||
game.engine.connect(render);
|
||||
game.engine.connect(sound);
|
||||
game.start(state).then(onGameStateChange).endThen(onGameComplete);
|
||||
game.connect(this);
|
||||
if (panel != null) {
|
||||
game.connect(panel);
|
||||
}
|
||||
game.start(state);
|
||||
timer = new Timer(10);
|
||||
timer.run = updateEngine;
|
||||
if (panel != null) {
|
||||
panel.game = game;
|
||||
}
|
||||
content.addEventListener(Event.ENTER_FRAME, _redraw);
|
||||
render.draw(game.engine);
|
||||
sound.play('start');
|
||||
@@ -68,17 +69,15 @@ class GameFrame extends GroupView {
|
||||
render.reset();
|
||||
}
|
||||
|
||||
private function onGameStateChange(_):Void {
|
||||
if (panel != null) {
|
||||
panel.toUpdate();
|
||||
}
|
||||
}
|
||||
public function onGameStart(state:GameState):Void {}
|
||||
|
||||
private function onGameComplete(_):Void {
|
||||
public function onGameChange(state:GameState):Void {}
|
||||
|
||||
public function onGameComplete(state:GameState):Void {
|
||||
result = state;
|
||||
state = switch game.next() {
|
||||
case Option.Some(s): s;
|
||||
case Option.None: null;
|
||||
case Some(s): s;
|
||||
case None: null;
|
||||
}
|
||||
stop();
|
||||
switcher.change(ResultFrame.ID);
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package ru.m.tankz.view.common;
|
||||
|
||||
import ru.m.tankz.game.Game;
|
||||
import haxework.view.IView;
|
||||
import ru.m.tankz.game.GameState;
|
||||
|
||||
interface IGamePanel extends IView<Dynamic> {
|
||||
public var game:Game;
|
||||
public function onGameStart(state:GameState):Void;
|
||||
public function onGameChange(state:GameState):Void;
|
||||
public function onGameComplete(state:GameState):Void;
|
||||
}
|
||||
|
||||
@@ -1,25 +1,37 @@
|
||||
package ru.m.tankz.view.common;
|
||||
|
||||
import openfl.Assets;
|
||||
import haxework.view.LabelView;
|
||||
import haxework.view.ImageView;
|
||||
import haxework.view.HGroupView;
|
||||
import haxework.view.ImageView;
|
||||
import haxework.view.LabelView;
|
||||
import openfl.Assets;
|
||||
import ru.m.tankz.game.GameState.PlayerState;
|
||||
import ru.m.tankz.game.GameState;
|
||||
|
||||
@:template class LifeView extends HGroupView {
|
||||
@:provide static var currentState:GameState;
|
||||
|
||||
@:view("tank") public var tankImage:ImageView;
|
||||
@:view("live") public var liveLabel:LabelView;
|
||||
@:view("life") public var lifeLabel:LabelView;
|
||||
@:view("score") public var scoreLabel:LabelView;
|
||||
|
||||
public var state(null, set):PlayerState;
|
||||
public var tank(null, set):String;
|
||||
public var color(null, set):Int;
|
||||
public var live(null, set):Int;
|
||||
public var life(null, set):Int;
|
||||
public var score(null, set):Int;
|
||||
|
||||
private inline function set_state(value:PlayerState):PlayerState {
|
||||
state = value;
|
||||
toUpdate();
|
||||
return state;
|
||||
}
|
||||
|
||||
private inline function set_tank(value:String):String {
|
||||
if (value != null) {
|
||||
tankImage.image = Assets.getBitmapData('resources/image/tank/${value}-0.png');
|
||||
if (value != null && value != tank) {
|
||||
tank = value;
|
||||
tankImage.image = Assets.getBitmapData('resources/image/tank/${tank}-0.png');
|
||||
}
|
||||
return value;
|
||||
return tank;
|
||||
}
|
||||
|
||||
private inline function set_color(value:Int):Int {
|
||||
@@ -27,8 +39,8 @@ import haxework.view.HGroupView;
|
||||
return value;
|
||||
}
|
||||
|
||||
private inline function set_live(value:Int):Int {
|
||||
liveLabel.text = '${value}';
|
||||
private inline function set_life(value:Int):Int {
|
||||
lifeLabel.text = '${value}';
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -36,4 +48,21 @@ import haxework.view.HGroupView;
|
||||
scoreLabel.text = '${value}$';
|
||||
return value;
|
||||
}
|
||||
|
||||
override public function update():Void {
|
||||
super.update();
|
||||
if (state != null) {
|
||||
var tankConfig = currentState.config.getTank(state.tank);
|
||||
tank = tankConfig == null ? 'ba' : tankConfig.skin;
|
||||
color = currentState.config.getColor(state.id);
|
||||
life = state.life;
|
||||
score = state.score;
|
||||
}
|
||||
}
|
||||
|
||||
public static inline function factory(index:Int, data:PlayerState):LifeView {
|
||||
var result = new LifeView();
|
||||
result.state = data;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ layout.margin: 5
|
||||
views:
|
||||
- id: tank
|
||||
$type: haxework.view.ImageView
|
||||
- id: live
|
||||
- id: life
|
||||
$type: haxework.view.LabelView
|
||||
skinId: text.box
|
||||
geometry.size.fixed: [50, 38]
|
||||
|
||||
@@ -9,13 +9,13 @@ import ru.m.tankz.view.common.IGamePanel;
|
||||
public static inline var ID = "death.game";
|
||||
|
||||
@:view("render") private var renderView(default, null):Render;
|
||||
//@:view("panel") private var panelView(default, null):IGamePanel;
|
||||
@:view("panel") private var panelView(default, null):IGamePanel;
|
||||
|
||||
override private function get_render():Render {
|
||||
return renderView;
|
||||
}
|
||||
|
||||
/*override private function get_panel():IGamePanel {
|
||||
override private function get_panel():IGamePanel {
|
||||
return panelView;
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
---
|
||||
skinId: container
|
||||
views:
|
||||
- $type: haxework.view.VGroupView
|
||||
- $type: haxework.view.HGroupView
|
||||
layout.margin: 5
|
||||
views:
|
||||
- id: render
|
||||
$type: ru.m.tankz.render.Render
|
||||
- id: panel
|
||||
$type: ru.m.tankz.view.death.DeathGamePanel
|
||||
|
||||
30
src/client/haxe/ru/m/tankz/view/death/DeathGamePanel.hx
Normal file
30
src/client/haxe/ru/m/tankz/view/death/DeathGamePanel.hx
Normal file
@@ -0,0 +1,30 @@
|
||||
package ru.m.tankz.view.death;
|
||||
|
||||
import ru.m.tankz.game.GameState;
|
||||
import haxework.view.DataView;
|
||||
import haxework.view.LabelView;
|
||||
import haxework.view.VGroupView;
|
||||
import ru.m.tankz.game.GameState.PlayerState;
|
||||
import ru.m.tankz.view.common.IGamePanel;
|
||||
import ru.m.tankz.view.common.LifeView;
|
||||
|
||||
@:template class DeathGamePanel extends VGroupView implements IGamePanel {
|
||||
|
||||
@:view var level:LabelView;
|
||||
@:view var players:DataView<PlayerState, LifeView>;
|
||||
|
||||
public function onGameStart(state:GameState):Void {
|
||||
level.text = 'Level ${state.level}';
|
||||
players.data = state.players;
|
||||
}
|
||||
|
||||
public function onGameChange(state:GameState):Void {
|
||||
for (view in players.views) {
|
||||
view.toUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
public function onGameComplete(state:GameState):Void {
|
||||
|
||||
}
|
||||
}
|
||||
18
src/client/haxe/ru/m/tankz/view/death/DeathGamePanel.yaml
Normal file
18
src/client/haxe/ru/m/tankz/view/death/DeathGamePanel.yaml
Normal file
@@ -0,0 +1,18 @@
|
||||
---
|
||||
geometry.padding: 5
|
||||
geometry.size.height: 100%
|
||||
layout.margin: 5
|
||||
layout.hAlign: right
|
||||
views:
|
||||
- id: level
|
||||
$type: haxework.view.LabelView
|
||||
skinId: text.box
|
||||
geometry.size.height: 38
|
||||
geometry.size.width: 100%
|
||||
- $type: haxework.view.SpriteView
|
||||
geometry.size.height: 50%
|
||||
- id: players
|
||||
$type: haxework.view.DataView
|
||||
layout:
|
||||
$type: haxework.view.layout.VerticalLayout
|
||||
factory: $code:ru.m.tankz.view.common.LifeView.factory
|
||||
@@ -1,11 +1,11 @@
|
||||
package ru.m.tankz.view.dota;
|
||||
|
||||
import ru.m.tankz.preset.DotaGame;
|
||||
import haxework.view.HGroupView;
|
||||
import haxework.view.LabelView;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.view.common.IGamePanel;
|
||||
import ru.m.tankz.view.common.LifeView;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.preset.DotaGame;
|
||||
|
||||
@:template class DotaGamePanel extends HGroupView implements IGamePanel {
|
||||
|
||||
@@ -13,20 +13,18 @@ import ru.m.tankz.preset.DotaGame;
|
||||
@:view var dire:LifeView;
|
||||
@:view var level:LabelView;
|
||||
|
||||
public var game:Game;
|
||||
|
||||
private function updateViews():Void {
|
||||
level.text = 'Level ${game.state.level}';
|
||||
radiant.live = game.teams[DotaGame.RADIANT].life;
|
||||
radiant.score = game.teams[DotaGame.RADIANT].score;
|
||||
dire.live = game.teams[DotaGame.DIRE].life;
|
||||
dire.score = game.teams[DotaGame.DIRE].score;
|
||||
public function onGameStart(state:GameState):Void {
|
||||
level.text = 'Level ${state.level}';
|
||||
}
|
||||
|
||||
override public function update():Void {
|
||||
if (game != null) {
|
||||
updateViews();
|
||||
}
|
||||
super.update();
|
||||
public function onGameChange(state:GameState):Void {
|
||||
radiant.life = state.getTeamLife(DotaGame.RADIANT);
|
||||
radiant.score = state.getTeamScore(DotaGame.RADIANT);
|
||||
dire.life = state.getTeamLife(DotaGame.DIRE);
|
||||
dire.score = state.getTeamScore(DotaGame.DIRE);
|
||||
}
|
||||
|
||||
public function onGameComplete(state:GameState):Void {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user