[common] add GameStart
This commit is contained in:
@@ -15,7 +15,6 @@ import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.proto.pack.Request;
|
||||
import ru.m.tankz.proto.pack.Response;
|
||||
import ru.m.tankz.sound.SoundManager;
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
import ru.m.tankz.storage.UserStorage;
|
||||
#if flash
|
||||
import flash.Lib;
|
||||
@@ -46,7 +45,6 @@ class Init {
|
||||
Provider.setFactory(IResources, Resources);
|
||||
Provider.setFactory(ILevelBundle, LevelBundle);
|
||||
Provider.setFactory(IConfigBundle, ConfigBundle);
|
||||
Provider.setFactory(SaveStorage, SaveStorage);
|
||||
Provider.setFactory(UserStorage, UserStorage);
|
||||
Provider.setFactory(SettingsStorage, SettingsStorage);
|
||||
Provider.setFactory(SoundManager, SoundManager);
|
||||
|
||||
@@ -5,14 +5,12 @@ import ru.m.tankz.frame.dota.DotaLevelFrame;
|
||||
import haxework.gui.ButtonView;
|
||||
import haxework.gui.frame.FrameSwitcher;
|
||||
import haxework.gui.VGroupView;
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
|
||||
@:template class StartFrame extends VGroupView {
|
||||
|
||||
public static var ID(default, never):String = "start";
|
||||
|
||||
@:provide var frameSwitcher:FrameSwitcher;
|
||||
@:provide var storage:SaveStorage;
|
||||
|
||||
public function onPress(view:ButtonView):Void {
|
||||
switch (view.id) {
|
||||
|
||||
@@ -15,12 +15,15 @@ import ru.m.tankz.preset.ClassicGame;
|
||||
public var game:Game;
|
||||
|
||||
private function updateViews():Void {
|
||||
bot.count.text = '${game.teams[ClassicGame.BOT].life}';
|
||||
player1.count.text = '${game.teams[ClassicGame.HUMAN].players[0].state.life}';
|
||||
bot.live.text = '${game.teams[ClassicGame.BOT].life}';
|
||||
player1.live.text = '${game.teams[ClassicGame.HUMAN].players[0].state.life}';
|
||||
player1.score.text = '${game.teams[ClassicGame.HUMAN].players[0].state.score}';
|
||||
if (game.teams[ClassicGame.HUMAN].players[1] != null) {
|
||||
player2.count.text = '${game.teams[ClassicGame.HUMAN].players[1].state.life}';
|
||||
player2.live.text = '${game.teams[ClassicGame.HUMAN].players[1].state.life}';
|
||||
player2.score.text = '${game.teams[ClassicGame.HUMAN].players[1].state.score}';
|
||||
} else {
|
||||
player2.count.text = "";
|
||||
player2.live.text = "";
|
||||
player2.score.text = "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,11 +7,10 @@ import haxework.gui.frame.FrameSwitcher;
|
||||
import haxework.gui.GroupView;
|
||||
import ru.m.tankz.frame.common.IGamePanel;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.game.GameStart;
|
||||
import ru.m.tankz.network.NetworkManager;
|
||||
import ru.m.tankz.render.Render;
|
||||
import ru.m.tankz.sound.SoundManager;
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
|
||||
class GameFrame extends GroupView {
|
||||
|
||||
@@ -22,8 +21,7 @@ class GameFrame extends GroupView {
|
||||
|
||||
@:provide var network:NetworkManager;
|
||||
@:provide var sound:SoundManager;
|
||||
@:provide var state:GameState;
|
||||
@:provide var storage:SaveStorage;
|
||||
@:provide var gameStart:GameStart;
|
||||
@:provide var switcher:FrameSwitcher;
|
||||
|
||||
private var game:Game;
|
||||
@@ -38,14 +36,14 @@ class GameFrame extends GroupView {
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
start(state);
|
||||
start(gameStart);
|
||||
}
|
||||
|
||||
private function start(state:GameState):Void {
|
||||
game = new Game(state.type);
|
||||
private function start(start:GameStart):Void {
|
||||
game = new Game(start.type);
|
||||
game.engine.connect(render);
|
||||
game.engine.connect(sound);
|
||||
game.start(state).then(onGameStateChange).endThen(onGameComplete);
|
||||
game.start(start).then(onGameStateChange).endThen(onGameComplete);
|
||||
timer = new Timer(10);
|
||||
timer.run = updateEngine;
|
||||
panel.game = game;
|
||||
@@ -67,24 +65,15 @@ class GameFrame extends GroupView {
|
||||
render.reset();
|
||||
}
|
||||
|
||||
private function onGameStateChange(s:GameState):GameState {
|
||||
private function onGameStateChange(_):Void {
|
||||
panel.toUpdate();
|
||||
return s;
|
||||
}
|
||||
|
||||
private function onGameComplete(result:Option<GameState>):Void {
|
||||
switch (result) {
|
||||
case Option.Some(s):
|
||||
panel.toUpdate();
|
||||
case Option.None:
|
||||
}
|
||||
private function onGameComplete(_):Void {
|
||||
switch (game.next()) {
|
||||
case Option.Some(s):
|
||||
var state = game.save();
|
||||
this.state = state;
|
||||
storage.write(state);
|
||||
stop();
|
||||
start(state);
|
||||
start(s);
|
||||
case Option.None:
|
||||
switcher.change(StartFrame.ID);
|
||||
}
|
||||
|
||||
@@ -6,8 +6,7 @@ import haxework.resources.IResources;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
import ru.m.tankz.game.GameStart;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
class LevelFrame extends GroupView {
|
||||
@@ -18,12 +17,11 @@ class LevelFrame extends GroupView {
|
||||
private var config(default, null):Config;
|
||||
|
||||
@:provide var configBundle:IConfigBundle;
|
||||
@:provide var state:GameState;
|
||||
@:provide var storage:SaveStorage;
|
||||
@:provide var start:GameStart;
|
||||
@:provide var resources:IResources;
|
||||
|
||||
private function set_gameType(value:GameType):GameType {
|
||||
if (gameType != value) {
|
||||
if (gameType != value || true) { // ToDo:
|
||||
gameType = value;
|
||||
config = configBundle.get(gameType);
|
||||
preset = config.presets[0];
|
||||
@@ -32,13 +30,13 @@ class LevelFrame extends GroupView {
|
||||
}
|
||||
|
||||
private function set_preset(value:GamePreset):GamePreset {
|
||||
if (preset != value) {
|
||||
if (preset != value || true) { // ToDo:
|
||||
preset = value;
|
||||
state = new GameState(gameType, preset.id);
|
||||
start = new GameStart(gameType, preset.id);
|
||||
for (team in value.teams) {
|
||||
for (player in team.players) {
|
||||
var playerId = new PlayerId(team.id, player.index);
|
||||
state.control.set(playerId, player.control != null ? player.control : Control.BOT);
|
||||
start.players.push({playerId: playerId, control: player.control != null ? player.control : Control.BOT});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,8 +44,8 @@ class LevelFrame extends GroupView {
|
||||
}
|
||||
|
||||
private function set_level(value:Int):Int {
|
||||
state.level = value;
|
||||
return state.level;
|
||||
start.level = value;
|
||||
return start.level;
|
||||
}
|
||||
|
||||
private function levelViewFactory(index:Int, level:Int):ButtonView {
|
||||
|
||||
@@ -6,5 +6,6 @@ import haxework.gui.HGroupView;
|
||||
|
||||
@:template class LifeView extends HGroupView {
|
||||
@:view public var image:ImageView;
|
||||
@:view public var count:LabelView;
|
||||
@:view public var live:LabelView;
|
||||
@:view public var score:LabelView;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,11 @@ layout.margin: 5
|
||||
views:
|
||||
- id: image
|
||||
$type: haxework.gui.ImageView
|
||||
- id: count
|
||||
- id: live
|
||||
$type: haxework.gui.LabelView
|
||||
skinId: text.box
|
||||
geometry.size.fixed: [50, 38]
|
||||
- id: score
|
||||
$type: haxework.gui.LabelView
|
||||
skinId: text.box
|
||||
geometry.size.fixed: [100, 38]
|
||||
|
||||
@@ -1,65 +1,93 @@
|
||||
package ru.m.tankz.frame.common;
|
||||
|
||||
import haxework.color.Color;
|
||||
import haxework.gui.ButtonView;
|
||||
import haxework.gui.DataView;
|
||||
import haxework.gui.HGroupView;
|
||||
import haxework.gui.LabelView;
|
||||
import haxework.gui.list.ListView;
|
||||
import haxework.gui.skin.Skin;
|
||||
import openfl.Assets;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.Type;
|
||||
import haxework.gui.skin.ISkin;
|
||||
import haxework.gui.ToggleButtonView;
|
||||
import ru.m.tankz.game.GameStart;
|
||||
import ru.m.tankz.Type.PlayerId;
|
||||
import ru.m.tankz.Type.TeamId;
|
||||
|
||||
@:template class PlayerView extends HGroupView implements IListItemView<PlayerId> {
|
||||
class TeamButton extends ToggleButtonView {
|
||||
public var team(default, set):TeamId;
|
||||
|
||||
public var item_index(default, default):Int;
|
||||
public var data(default, set):PlayerId;
|
||||
|
||||
@:view("index") var indexLabel(default, null):LabelView;
|
||||
@:view var control(default, null):ButtonView;
|
||||
|
||||
@:provide var state:GameState;
|
||||
@:provide var configBundle:IConfigBundle;
|
||||
|
||||
private function set_data(value:PlayerId):PlayerId {
|
||||
data = value;
|
||||
indexLabel.text = '${value.team} ${Std.string(data.index + 1)}';
|
||||
var color = 0xffffff;
|
||||
var config = configBundle.get(state.type);
|
||||
var preset = config.getPreset(state.presetId);
|
||||
for (team in preset.teams) {
|
||||
if (team.id == data.team) {
|
||||
color = team.color;
|
||||
for (player in team.players) {
|
||||
if (player.index == data.index) {
|
||||
if (!player.color.zero) {
|
||||
color = player.color;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
preset.teams;
|
||||
}
|
||||
private function set_team(value:TeamId):TeamId {
|
||||
if (team != value) {
|
||||
team = value;
|
||||
text = team.substr(0, 1).toUpperCase() + team.substr(1);
|
||||
}
|
||||
indexLabel.fontColor = color;
|
||||
var controlType = state.control.get(value);
|
||||
var image = Assets.getBitmapData('resources/image/ui/control/${controlType}.png');
|
||||
control.skin = [Skin.buttonBitmap(image)];
|
||||
indexLabel.update();
|
||||
return team;
|
||||
}
|
||||
}
|
||||
|
||||
class TeamSkin implements ISkin<TeamButton> {
|
||||
|
||||
public var color(default, default):Int;
|
||||
|
||||
public function new(color:Int) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public function draw(view:TeamButton):Void {
|
||||
view.fontColor = view.on ? 0x000000 : 0xcccccc;
|
||||
var graphics = view.content.graphics;
|
||||
graphics.beginFill(view.on ? color : 0x333333);
|
||||
graphics.lineStyle(1, view.on ? 0x333333 : color);
|
||||
graphics.drawRect(0, 0, view.width, view.height);
|
||||
graphics.endFill();
|
||||
graphics.lineStyle();
|
||||
}
|
||||
}
|
||||
|
||||
@:template class PlayerView extends HGroupView {
|
||||
private static inline var NONE:TeamId = "none";
|
||||
|
||||
public var item_index(default, set):Int;
|
||||
public var data(default, set):PlayerStart;
|
||||
|
||||
@:view var label(default, null):LabelView;
|
||||
@:view var teams(default, null):DataView<TeamId, ToggleButtonView>;
|
||||
|
||||
@:provide var start:GameStart;
|
||||
|
||||
private function teamViewFactory(index:Int, team:TeamId) {
|
||||
var view = new TeamButton();
|
||||
view.skin = [new TeamSkin(getTeamColor(team))];
|
||||
view.geometry.padding = [10, 5];
|
||||
view.team = team;
|
||||
view.on = team == NONE;
|
||||
return view;
|
||||
}
|
||||
|
||||
private function set_data(value:PlayerStart):PlayerStart {
|
||||
data = value;
|
||||
teams.data = [NONE].concat([for (team in start.preset.teams) team.id]);
|
||||
return data;
|
||||
}
|
||||
|
||||
public function toggleControl():Void {
|
||||
if (data != null && data.index > -1) {
|
||||
var controlType = switch state.control.get(data) {
|
||||
case Control.BOT: Control.HUMAN;
|
||||
case Control.HUMAN: Control.BOT;
|
||||
case _: Control.BOT;
|
||||
private function getTeamColor(teamId:TeamId):Int {
|
||||
var color = 0xcccccc;
|
||||
for (team in start.preset.teams) {
|
||||
if (team.id == teamId) {
|
||||
if (!team.color.zero) color = team.color;
|
||||
break;
|
||||
}
|
||||
state.control.set(data, controlType);
|
||||
this.data = data;
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
private function set_item_index(value:Int):Int {
|
||||
item_index = value;
|
||||
label.text = 'Player ${item_index}';
|
||||
return item_index;
|
||||
}
|
||||
|
||||
private function onTeamSelect(team:TeamId) {
|
||||
data.playerId = new PlayerId(team, item_index);
|
||||
for (view in teams.views) {
|
||||
var button = cast(view, TeamButton);
|
||||
button.on = team == button.team;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
---
|
||||
geometry.size.width: 200
|
||||
geometry.size.height: 44
|
||||
geometry.margin: 5
|
||||
layout.margin: 10
|
||||
layout.vAlign: middle
|
||||
views:
|
||||
- id: index
|
||||
- id: label
|
||||
$type: haxework.gui.LabelView
|
||||
geometry.size.stretch: true
|
||||
skin:
|
||||
- $type: haxework.gui.skin.ColorSkin
|
||||
color: 0x000000
|
||||
alpha: 0.2
|
||||
shadow: true
|
||||
shadowColor: 0x000000
|
||||
- id: control
|
||||
$type: haxework.gui.ButtonView
|
||||
+onPress: $code:toggleControl()
|
||||
skinId: text
|
||||
- id: teams
|
||||
$type: haxework.gui.DataView
|
||||
factory: $this:teamViewFactory
|
||||
layout:
|
||||
$type: haxework.gui.layout.HorizontalLayout
|
||||
margin: 3
|
||||
+onDataSelect: $this:onTeamSelect
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package ru.m.tankz.frame.dota;
|
||||
|
||||
import ru.m.tankz.preset.DotaGame;
|
||||
import ru.m.tankz.frame.common.LifeView;
|
||||
import ru.m.tankz.frame.common.IGamePanel;
|
||||
import haxework.gui.HGroupView;
|
||||
import ru.m.tankz.frame.common.IGamePanel;
|
||||
import ru.m.tankz.frame.common.LifeView;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.preset.DotaGame;
|
||||
|
||||
@:template class DotaGamePanel extends HGroupView implements IGamePanel {
|
||||
|
||||
@@ -13,31 +13,11 @@ import ru.m.tankz.game.Game;
|
||||
|
||||
public var game:Game;
|
||||
|
||||
private function stateString(game:Game):String {
|
||||
if (game == null) {
|
||||
return '';
|
||||
}
|
||||
var result:Array<String> = [];
|
||||
result.push('Level: ${game.state.level}');
|
||||
for (team in game.teams) {
|
||||
if (game.loser == team.id) {
|
||||
result.push('${team.id}: LOSE');
|
||||
} else if (team.life > 0) {
|
||||
result.push('${team.id}: ${team.life}');
|
||||
} else {
|
||||
for (player in team.players) {
|
||||
if (player.state.life > 0) {
|
||||
result.push('${player.id.team}${player.id.index}: ${player.state.life}');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return '[ ${result.join(' | ')} ]';
|
||||
}
|
||||
|
||||
private function updateViews():Void {
|
||||
radiant.count.text = '${game.teams[DotaGame.RADIANT].life}';
|
||||
dire.count.text = '${game.teams[DotaGame.DIRE].life}';
|
||||
radiant.live.text = '${game.teams[DotaGame.RADIANT].life}';
|
||||
radiant.score.text = '0'; // ToDO
|
||||
dire.live.text = '${game.teams[DotaGame.DIRE].life}';
|
||||
dire.score.text = '0'; // ToDO
|
||||
}
|
||||
|
||||
override public function update():Void {
|
||||
|
||||
@@ -3,16 +3,17 @@ package ru.m.tankz.frame.dota;
|
||||
import haxework.gui.ButtonView;
|
||||
import haxework.gui.DataView;
|
||||
import haxework.gui.frame.FrameSwitcher;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.frame.common.LevelFrame;
|
||||
import ru.m.tankz.frame.common.PlayerView;
|
||||
import ru.m.tankz.game.GameStart.PlayerStart;
|
||||
import ru.m.tankz.preset.DotaGame;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
@:template class DotaLevelFrame extends LevelFrame {
|
||||
public static inline var ID = "dota.level";
|
||||
|
||||
@:view var levels(default, null):DataView<Int, ButtonView>;
|
||||
@:view var players(default, null):DataView<PlayerId, PlayerView>;
|
||||
@:view var players(default, null):DataView<PlayerStart, PlayerView>;
|
||||
|
||||
@:provide var frames:FrameSwitcher;
|
||||
|
||||
@@ -20,15 +21,17 @@ import ru.m.tankz.Type;
|
||||
gameType = DotaGame.TYPE;
|
||||
levels.data = [for (i in 0...config.game.levels) i];
|
||||
var data = [];
|
||||
for (team in preset.teams) {
|
||||
for (p in team.players) {
|
||||
data.push(new PlayerId(team.id, p.index));
|
||||
}
|
||||
for (i in 0...2) {
|
||||
data.push({
|
||||
playerId: null,
|
||||
control: Control.HUMAN,
|
||||
});
|
||||
}
|
||||
start.players = data;
|
||||
players.data = data;
|
||||
}
|
||||
|
||||
private function playerViewFactory(index:Int, player:PlayerId):PlayerView {
|
||||
private function playerViewFactory(index:Int, player:PlayerStart):PlayerView {
|
||||
var view = new PlayerView();
|
||||
view.item_index = index;
|
||||
view.data = player;
|
||||
|
||||
@@ -6,21 +6,19 @@ views:
|
||||
- $type: haxework.gui.LabelView
|
||||
skinId: text.header
|
||||
text: DotA
|
||||
- $type: haxework.gui.HGroupView
|
||||
views:
|
||||
- id: players
|
||||
$type: haxework.gui.DataView
|
||||
layout:
|
||||
$type: haxework.gui.layout.VerticalLayout
|
||||
hAlign: center
|
||||
factory: $this:playerViewFactory
|
||||
geometry.padding: 10
|
||||
- id: levels
|
||||
$type: haxework.gui.DataView
|
||||
layout:
|
||||
$type: haxework.gui.layout.TailLayout
|
||||
rowSize: 5
|
||||
margin: 5
|
||||
factory: $this:levelViewFactory
|
||||
+onDataSelect: $code:function(value) level = value
|
||||
geometry.padding: 10
|
||||
- id: players
|
||||
$type: haxework.gui.DataView
|
||||
layout:
|
||||
$type: haxework.gui.layout.VerticalLayout
|
||||
hAlign: center
|
||||
factory: $this:playerViewFactory
|
||||
geometry.padding: 10
|
||||
- id: levels
|
||||
$type: haxework.gui.DataView
|
||||
layout:
|
||||
$type: haxework.gui.layout.TailLayout
|
||||
rowSize: 5
|
||||
margin: 5
|
||||
factory: $this:levelViewFactory
|
||||
+onDataSelect: $code:function(value) level = value
|
||||
geometry.padding: 10
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
package ru.m.tankz.storage;
|
||||
|
||||
import flash.net.SharedObject;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
|
||||
class SaveStorage {
|
||||
|
||||
private static var TAG(default, never):String = 'SaveStorage';
|
||||
|
||||
private var so:SharedObject;
|
||||
|
||||
public function new() {
|
||||
so = SharedObject.getLocal('tankz');
|
||||
}
|
||||
|
||||
public function read(type:GameType):Null<GameState> {
|
||||
var data:String = Reflect.getProperty(so.data, type);
|
||||
L.d(TAG, 'read: ${data}');
|
||||
if (data != null) {
|
||||
return GameState.fromYaml(data);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function write(save:GameState):Void {
|
||||
var data:String = save.toYaml();
|
||||
L.d(TAG, 'write: ${data}');
|
||||
so.setProperty(save.type, data);
|
||||
so.flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user