[common] added GameSave
This commit is contained in:
@@ -13,11 +13,16 @@ import haxework.provider.Provider;
|
||||
import haxework.resources.IResources;
|
||||
import haxework.resources.Resources;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.tankz.bundle.ConfigBundle;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.bundle.ILevelBundle;
|
||||
import ru.m.tankz.bundle.LevelBundle;
|
||||
import ru.m.tankz.frame.StartFrame;
|
||||
import ru.m.tankz.game.ClassicGame;
|
||||
import ru.m.tankz.game.DotaGame;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.PacketBuilder;
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
#if flash import haxework.log.JSLogger; #end
|
||||
#if debug import haxework.log.SocketLogger; #end
|
||||
|
||||
@@ -71,7 +76,6 @@ class Client implements IConnectionHandler {
|
||||
Root.bind(view);
|
||||
view.content.stage.stageFocusRect = false;
|
||||
//view.logout.onPress = this;
|
||||
view.switcher.change(StartFrame.ID);
|
||||
|
||||
view.content.stage.addEventListener(KeyboardEvent.KEY_UP, function(event:KeyboardEvent):Void {
|
||||
if (event.keyCode == Keyboard.ESCAPE) {
|
||||
@@ -79,8 +83,13 @@ class Client implements IConnectionHandler {
|
||||
}
|
||||
});
|
||||
|
||||
Provider.setFactory(IConfigBundle, ConfigBundle);
|
||||
Provider.setFactory(ILevelBundle, LevelBundle);
|
||||
Provider.setFactory(SaveStorage, SaveStorage);
|
||||
Provider.setFactory(Game, ClassicGame, ClassicGame.TYPE);
|
||||
Provider.setFactory(Game, DotaGame, DotaGame.TYPE);
|
||||
|
||||
view.switcher.change(StartFrame.ID);
|
||||
}
|
||||
|
||||
public function onPress(view:ButtonView):Void {
|
||||
|
||||
30
src/client/haxe/ru/m/tankz/bundle/ConfigBundle.hx
Normal file
30
src/client/haxe/ru/m/tankz/bundle/ConfigBundle.hx
Normal file
@@ -0,0 +1,30 @@
|
||||
package ru.m.tankz.bundle;
|
||||
|
||||
import openfl.Assets;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.Type;
|
||||
import yaml.Parser;
|
||||
import yaml.Yaml;
|
||||
|
||||
|
||||
typedef ConfigSource = {
|
||||
var game:GameConfig;
|
||||
var map: MapConfig;
|
||||
var bricks: Array<BrickConfig>;
|
||||
var presets: Array<GamePreset>;
|
||||
var points: Array<SpawnPoint>;
|
||||
var tanks: Array<TankConfig>;
|
||||
var bonuses: Array<BonusConfig>;
|
||||
}
|
||||
|
||||
class ConfigBundle implements IConfigBundle {
|
||||
|
||||
private static function convert(raw:Dynamic):ConfigSource {
|
||||
return raw;
|
||||
}
|
||||
|
||||
public function get(type:GameType):Config {
|
||||
var source = convert(Yaml.parse(Assets.getText('resources/${type}/config.yaml'), Parser.options().useObjects()));
|
||||
return new Config(type, source.game, source.map, source.bricks, source.presets, source.points, source.tanks, source.bonuses);
|
||||
}
|
||||
}
|
||||
15
src/client/haxe/ru/m/tankz/bundle/LevelBundle.hx
Normal file
15
src/client/haxe/ru/m/tankz/bundle/LevelBundle.hx
Normal file
@@ -0,0 +1,15 @@
|
||||
package ru.m.tankz.bundle;
|
||||
|
||||
import openfl.Assets;
|
||||
import ru.m.tankz.config.Config;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.util.LevelUtil;
|
||||
|
||||
|
||||
class LevelBundle implements ILevelBundle {
|
||||
|
||||
public function get(type:GameType, config:Config, level:Int):LevelConfig {
|
||||
var data:String = Assets.getText('resources/${type}/levels/level${LevelUtil.formatLevel(level)}.txt');
|
||||
return LevelUtil.loads(config, data);
|
||||
}
|
||||
}
|
||||
@@ -11,9 +11,11 @@ import haxework.provider.Provider;
|
||||
import protohx.Message;
|
||||
import ru.m.connect.IConnection;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.game.GameSave;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import ru.m.tankz.proto.pack.GameUpdateResponse;
|
||||
import ru.m.tankz.render.Render;
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
|
||||
|
||||
interface GameFrameLayout {
|
||||
@@ -34,16 +36,16 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
|
||||
public function init():Void {}
|
||||
|
||||
public function onShow():Void {
|
||||
start(Provider.get(GameState));
|
||||
start(Provider.get(GameSave));
|
||||
}
|
||||
|
||||
private function start(s:GameState):Void {
|
||||
game = Provider.build(Game, s.type);
|
||||
private function start(save:GameSave):Void {
|
||||
game = Provider.build(Game, save.state.type);
|
||||
if (game == null) {
|
||||
throw 'Unsupported game type "${s.type}"';
|
||||
throw 'Unsupported game type "${save.state.type}"';
|
||||
}
|
||||
game.engine.listeners.push(render);
|
||||
game.start(s).then(onGameStateChange).endThen(onGameComplete);
|
||||
game.start(save).then(onGameStateChange).endThen(onGameComplete);
|
||||
content.addEventListener(Event.ENTER_FRAME, redraw);
|
||||
//Provider.get(IConnection).packetHandler.addListener(this);
|
||||
render.draw(game.engine);
|
||||
@@ -67,6 +69,9 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
|
||||
}
|
||||
|
||||
private function stateString(game:Game):String {
|
||||
if (game.state == null) {
|
||||
return '';
|
||||
}
|
||||
var result:Array<String> = [];
|
||||
result.push('Level: ${game.state.level}');
|
||||
for (team in game.teams) {
|
||||
@@ -98,8 +103,10 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
|
||||
}
|
||||
switch (game.next()) {
|
||||
case Option.Some(s):
|
||||
var save = game.save();
|
||||
Provider.get(SaveStorage).write(save);
|
||||
stop();
|
||||
start(s);
|
||||
start(save);
|
||||
case Option.None:
|
||||
Provider.get(IFrameSwitcher).change(StartFrame.ID);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
package ru.m.tankz.frame;
|
||||
|
||||
import ru.m.tankz.config.ConfigBundle;
|
||||
import ru.m.tankz.game.GameSave;
|
||||
import haxework.gui.frame.IFrameSwitcher;
|
||||
import ru.m.tankz.game.GameState;
|
||||
import haxework.provider.Provider;
|
||||
import haxework.gui.list.ListView;
|
||||
import haxework.gui.ViewBuilder;
|
||||
import haxework.gui.VGroupView;
|
||||
import haxework.gui.ViewBuilder;
|
||||
import haxework.provider.Provider;
|
||||
import ru.m.tankz.bundle.IConfigBundle;
|
||||
import ru.m.tankz.game.GameState;
|
||||
|
||||
|
||||
interface LevelFrameLayout {
|
||||
@@ -23,13 +24,13 @@ class LevelFrame extends VGroupView implements ViewBuilder implements LevelFrame
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
var state = Provider.get(GameState);
|
||||
var c = ConfigBundle.get(state.type).game.levels;
|
||||
var state:GameState = Provider.get(GameSave).state;
|
||||
var c = Provider.get(IConfigBundle).get(state.type).game.levels;
|
||||
levels.data = [for (i in 0...c) i];
|
||||
}
|
||||
|
||||
public function onListItemClick(item:IListItemView<Int>):Void {
|
||||
Provider.get(GameState).level = item.data;
|
||||
Provider.get(GameSave).state.level = item.data;
|
||||
Provider.get(IFrameSwitcher).change(GameFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package ru.m.tankz.frame;
|
||||
|
||||
import ru.m.tankz.storage.SaveStorage;
|
||||
import ru.m.tankz.game.GameSave;
|
||||
import haxework.gui.ButtonView;
|
||||
import haxework.gui.frame.IFrameSwitcher;
|
||||
import haxework.gui.VGroupView;
|
||||
@@ -14,6 +16,7 @@ import ru.m.tankz.Type;
|
||||
interface StartFrameLayout {
|
||||
var classic_1p(default, null):ButtonView;
|
||||
var classic_2p(default, null):ButtonView;
|
||||
var classic_load(default, null):ButtonView;
|
||||
var dota_1p(default, null):ButtonView;
|
||||
var dota_2p_coop(default, null):ButtonView;
|
||||
var dota_2p_vs(default, null):ButtonView;
|
||||
@@ -24,20 +27,27 @@ class StartFrame extends VGroupView implements ViewBuilder implements StartFrame
|
||||
|
||||
public static inline var ID = "start";
|
||||
|
||||
public function init() {
|
||||
public function init():Void {
|
||||
classic_1p.onPress = this;
|
||||
classic_2p.onPress = this;
|
||||
classic_load.onPress = this;
|
||||
dota_1p.onPress = this;
|
||||
dota_2p_coop.onPress = this;
|
||||
dota_2p_vs.onPress = this;
|
||||
}
|
||||
|
||||
public function onShow():Void {
|
||||
classic_load.visible = Provider.get(SaveStorage).read(ClassicGame.TYPE) != null;
|
||||
}
|
||||
|
||||
public function onPress(view:ButtonView):Void {
|
||||
switch (view.id) {
|
||||
case 'classic_1p':
|
||||
startGame(ClassicGame.TYPE, ClassicGame.PLAYER1);
|
||||
case 'classic_2p':
|
||||
startGame(ClassicGame.TYPE, ClassicGame.PLAYER2);
|
||||
case 'classic_load':
|
||||
loadGame(ClassicGame.TYPE);
|
||||
case 'dota_1p':
|
||||
startGame(DotaGame.TYPE, DotaGame.PLAYER1);
|
||||
case 'dota_2p_coop':
|
||||
@@ -48,7 +58,13 @@ class StartFrame extends VGroupView implements ViewBuilder implements StartFrame
|
||||
}
|
||||
|
||||
private function startGame(type:GameType, presetId:PresetId):Void {
|
||||
Provider.set(GameState, new GameState(type, presetId));
|
||||
Provider.set(GameSave, new GameSave({type: type, presetId: presetId}));
|
||||
Provider.get(IFrameSwitcher).change(LevelFrame.ID);
|
||||
}
|
||||
|
||||
private function loadGame(type:GameType):Void {
|
||||
var save:GameSave = Provider.get(SaveStorage).read(type);
|
||||
Provider.set(GameSave, save);
|
||||
Provider.get(IFrameSwitcher).change(GameFrame.ID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,10 @@ views:
|
||||
$type: haxework.gui.ButtonView
|
||||
text: 2 Player
|
||||
$style: button
|
||||
- id: classic_load
|
||||
$type: haxework.gui.ButtonView
|
||||
text: Load
|
||||
$style: button
|
||||
- $type: haxework.gui.LabelView
|
||||
$style: label
|
||||
fontSize: 20
|
||||
|
||||
33
src/client/haxe/ru/m/tankz/storage/SaveStorage.hx
Normal file
33
src/client/haxe/ru/m/tankz/storage/SaveStorage.hx
Normal file
@@ -0,0 +1,33 @@
|
||||
package ru.m.tankz.storage;
|
||||
|
||||
import flash.net.SharedObject;
|
||||
import ru.m.tankz.Type;
|
||||
import ru.m.tankz.game.GameSave;
|
||||
|
||||
|
||||
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<GameSave> {
|
||||
var data:String = Reflect.getProperty(so.data, type);
|
||||
L.d(TAG, 'read: ${data}');
|
||||
if (data != null) {
|
||||
return GameSave.fromYaml(data);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function write(save:GameSave):Void {
|
||||
var data:String = save.toYaml();
|
||||
L.d(TAG, 'write: ${data}');
|
||||
so.setProperty(save.state.type, data);
|
||||
so.flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user