[common] remove GameSave; [client] update @:template macro use
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package ru.m.tankz;
|
||||
|
||||
import haxe.io.Bytes;
|
||||
import haxe.crypto.Crc32;
|
||||
|
||||
typedef Type = Dynamic;
|
||||
|
||||
@@ -15,9 +17,21 @@ typedef TankType = String;
|
||||
|
||||
typedef BonusType = String;
|
||||
|
||||
typedef PlayerId = {
|
||||
var team:TeamId;
|
||||
var index:Int;
|
||||
class PlayerId {
|
||||
public var team(default, null):TeamId;
|
||||
public var index(default, null):Int;
|
||||
|
||||
private var _hashCode(default, null):Int;
|
||||
|
||||
public function new(team:TeamId, index:Int) {
|
||||
this.team = team;
|
||||
this.index = index;
|
||||
this._hashCode = Crc32.make(Bytes.ofString('${team}-${index}'));
|
||||
}
|
||||
|
||||
public function hashCode():Int {
|
||||
return _hashCode;
|
||||
}
|
||||
}
|
||||
|
||||
typedef PresetId = String;
|
||||
|
||||
@@ -71,7 +71,6 @@ typedef TankSpawn = {
|
||||
|
||||
typedef PlayerConfig = {
|
||||
var index:Int;
|
||||
var control:ControlType;
|
||||
var tanks:Array<TankSpawn>;
|
||||
@:optional var bonus:Float;
|
||||
@:optional var protect:Float;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package ru.m.tankz.control;
|
||||
|
||||
import ru.m.tankz.game.Player;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
|
||||
interface IControlFactory {
|
||||
public function build(player:Player):Control;
|
||||
public function build(id:PlayerId, type:ControlType):Control;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ class Game {
|
||||
public var teams(default, null):Map<TeamId, Team>;
|
||||
public var config(default, null):Config;
|
||||
public var engine(default, null):Engine;
|
||||
public var loser(default, null):Null<TeamId>;
|
||||
|
||||
private var points:Array<SpawnPoint>;
|
||||
private var deferred:Deferred<GameState>;
|
||||
@@ -78,8 +79,9 @@ class Game {
|
||||
entity.rect.direction = Direction.fromString(point.direction);
|
||||
}
|
||||
|
||||
public function start(save:GameSave):Stream<GameState> {
|
||||
this.state = save.state;
|
||||
public function start(state:GameState):Stream<GameState> {
|
||||
this.state = state;
|
||||
this.loser = null;
|
||||
this.preset = config.getPreset(state.presetId);
|
||||
this.deferred = new Deferred();
|
||||
var level:LevelConfig = Provider.get(ILevelBundle).get(type, config, state.level);
|
||||
@@ -89,11 +91,12 @@ class Game {
|
||||
var controlFactory:IControlFactory = Provider.build(IControlFactory);
|
||||
for (teamConfig in preset.teams) {
|
||||
var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id);
|
||||
var team:Team = new Team(teamConfig, teamPoints, save);
|
||||
var team:Team = new Team(teamConfig, teamPoints, state);
|
||||
teams[team.id] = team;
|
||||
for (player in team.players.iterator()) {
|
||||
if (player.config.control != null) {
|
||||
var control = controlFactory.build(player);
|
||||
var controlType: ControlType = state.control.get(player.id);
|
||||
if (controlType != null) {
|
||||
var control = controlFactory.build(player.id, controlType);
|
||||
L.d(TAG, 'control(${player.id} - ${control})');
|
||||
if (control != null) {
|
||||
player.control = control;
|
||||
@@ -222,7 +225,7 @@ class Game {
|
||||
}
|
||||
|
||||
private function lose(teamId:TeamId):Void {
|
||||
state.loser = teamId;
|
||||
loser = teamId;
|
||||
complete();
|
||||
}
|
||||
|
||||
@@ -232,13 +235,13 @@ class Game {
|
||||
|
||||
public function next():Option<GameState> {
|
||||
for (rule in config.game.complete) {
|
||||
if (rule.team != null && rule.team == state.loser) {
|
||||
if (rule.team != null && rule.team == loser) {
|
||||
return Option.None;
|
||||
}
|
||||
}
|
||||
state.level++;
|
||||
if (state.level >= config.game.levels) state.level = 0;
|
||||
return Option.Some({type: state.type, presetId: preset.id, level: state.level});
|
||||
return Option.Some(state);
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
@@ -294,16 +297,7 @@ class Game {
|
||||
}
|
||||
}
|
||||
|
||||
public function save():GameSave {
|
||||
var save:GameSave = new GameSave(state);
|
||||
for (team in teams) {
|
||||
for (player in team.players) {
|
||||
if (player.config.control == Control.HUMAN) {
|
||||
player.state.life++;
|
||||
save.addPlayerState(player.id, player.state);
|
||||
}
|
||||
}
|
||||
}
|
||||
return save;
|
||||
public function save():GameState {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.game.Player;
|
||||
import ru.m.tankz.Type;
|
||||
import yaml.Parser;
|
||||
import yaml.Renderer;
|
||||
import yaml.Yaml;
|
||||
|
||||
|
||||
typedef PlayerSave = {
|
||||
var id:PlayerId;
|
||||
var state:PlayerState;
|
||||
}
|
||||
|
||||
enum GameServer {
|
||||
LOCAL;
|
||||
NETWORK;
|
||||
}
|
||||
|
||||
class GameSave {
|
||||
|
||||
public var state:GameState;
|
||||
public var players:Array<PlayerSave>;
|
||||
public var server:GameServer;
|
||||
|
||||
public function new(state:GameState, ?players:Array<PlayerSave>, ?server:GameServer) {
|
||||
this.state = {
|
||||
type: state.type,
|
||||
presetId: state.presetId,
|
||||
level: state.level,
|
||||
};
|
||||
this.players = players != null ? players : [];
|
||||
this.server = server != null ? server : GameServer.LOCAL;
|
||||
}
|
||||
|
||||
public function addPlayerState(id:PlayerId, state:PlayerState):Void {
|
||||
players.push({id: id, state: state});
|
||||
}
|
||||
|
||||
public function getPlayerState(id:PlayerId):PlayerState {
|
||||
for (player in players) {
|
||||
if (player.id.team == id.team && player.id.index == id.index) {
|
||||
return player.state;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function toYaml():String {
|
||||
return Yaml.render({state: state, players: players}, Renderer.options().setFlowLevel(0));
|
||||
}
|
||||
|
||||
public static function fromYaml(value:String):GameSave {
|
||||
var data:Dynamic = Yaml.parse(value, Parser.options().useObjects());
|
||||
return new GameSave(data.state, data.players);
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,33 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.game.Player.PlayerState;
|
||||
import haxe.ds.HashMap;
|
||||
import ru.m.tankz.Type;
|
||||
|
||||
|
||||
typedef GameState = {
|
||||
var type:GameType;
|
||||
var presetId:PresetId;
|
||||
@:optional var level:Int;
|
||||
@:optional var loser:TeamId;
|
||||
}
|
||||
class GameState {
|
||||
public var type(default, null):GameType;
|
||||
public var presetId(default, null):PresetId;
|
||||
public var control(default, null):HashMap<PlayerId, ControlType>;
|
||||
public var players(default, null):HashMap<PlayerId, PlayerState>;
|
||||
public var level(default, default):Int;
|
||||
|
||||
public function new(type:GameType, presetId:PresetId) {
|
||||
this.type = type;
|
||||
this.presetId = presetId;
|
||||
this.control = new HashMap();
|
||||
this.players = new HashMap();
|
||||
this.level = 0;
|
||||
}
|
||||
|
||||
public function toYaml():String {
|
||||
//return Yaml.render(this, Renderer.options().setFlowLevel(0));
|
||||
return "";
|
||||
}
|
||||
|
||||
public static function fromYaml(value:String):GameState {
|
||||
//var data:Dynamic = Yaml.parse(value, Parser.options().useObjects());
|
||||
//return new GameState();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ class Player {
|
||||
|
||||
public function new(teamId:TeamId, config:PlayerConfig) {
|
||||
this.config = config;
|
||||
this.id = {team:teamId, index:config.index};
|
||||
this.id = new PlayerId(teamId, config.index);
|
||||
this.control = null;
|
||||
this.state = {
|
||||
life: config.life,
|
||||
|
||||
@@ -14,13 +14,13 @@ class Team {
|
||||
public var isAlive(get, null):Bool;
|
||||
public var eagleId(default, default):Int;
|
||||
|
||||
public function new(config:TeamConfig, points:Array<SpawnPoint>, ?save:GameSave) {
|
||||
public function new(config:TeamConfig, points:Array<SpawnPoint>, ?state:GameState) {
|
||||
this.id = config.id;
|
||||
this.config = config;
|
||||
this.players = new Map();
|
||||
for (playerConfig in config.players) {
|
||||
var player:Player = new Player(id, playerConfig);
|
||||
var state = save.getPlayerState(player.id);
|
||||
var state = state.players.get(player.id);
|
||||
if (state != null) player.state = state;
|
||||
this.players[playerConfig.index] = player;
|
||||
}
|
||||
|
||||
@@ -21,13 +21,11 @@ bricks:
|
||||
|
||||
player:
|
||||
human: &human
|
||||
control: human
|
||||
life: 3
|
||||
protect: 5
|
||||
tanks:
|
||||
- {type: human0, rate: 1}
|
||||
bot: &bot
|
||||
control: bot
|
||||
color: 0xFFFFFF
|
||||
bonus: 0.25
|
||||
tanks:
|
||||
|
||||
@@ -20,29 +20,25 @@ bricks:
|
||||
|
||||
player:
|
||||
base: &player
|
||||
control: bot
|
||||
protect: 3
|
||||
fast: &player-fast
|
||||
<<: *player
|
||||
tanks:
|
||||
- {type: slow, rate: 0.5}
|
||||
- {type: fast, rate: 0.5}
|
||||
human1: &human1
|
||||
- {type: fast, rate: 1}
|
||||
slow: &player-slow
|
||||
<<: *player
|
||||
control: human
|
||||
color: 0xf055a0
|
||||
human2: &human2
|
||||
<<: *player
|
||||
control: human
|
||||
color: 0xa055f0
|
||||
tanks:
|
||||
- {type: slow, rate: 1}
|
||||
|
||||
team:
|
||||
base: &team
|
||||
life: 20
|
||||
players:
|
||||
- {<<: *player, index: 0}
|
||||
- {<<: *player, index: 1}
|
||||
- {<<: *player, index: 2}
|
||||
- {<<: *player, index: 3}
|
||||
- {<<: *player, index: 4}
|
||||
- {<<: *player-slow, index: 0}
|
||||
- {<<: *player-fast, index: 1}
|
||||
- {<<: *player-slow, index: 2}
|
||||
- {<<: *player-fast, index: 3}
|
||||
- {<<: *player-slow, index: 4}
|
||||
radiant: &radiant
|
||||
id: radiant
|
||||
color: 0xff4422
|
||||
@@ -53,45 +49,10 @@ team:
|
||||
<<: *team
|
||||
|
||||
presets:
|
||||
# player1
|
||||
- id: player1
|
||||
- id: default
|
||||
teams:
|
||||
- <<: *radiant
|
||||
players:
|
||||
- {<<: *human1, index: 0}
|
||||
- {<<: *player, index: 1}
|
||||
- {<<: *player, index: 2}
|
||||
- {<<: *player, index: 3}
|
||||
- {<<: *player, index: 4}
|
||||
- <<: *dire
|
||||
# player2_coop
|
||||
- id: player2_coop
|
||||
teams:
|
||||
- <<: *radiant
|
||||
players:
|
||||
- {<<: *human1, index: 0}
|
||||
- {<<: *human1, index: 1}
|
||||
- {<<: *player, index: 2}
|
||||
- {<<: *player, index: 3}
|
||||
- {<<: *player, index: 4}
|
||||
- <<: *dire
|
||||
# player2_vs
|
||||
- id: player2_vs
|
||||
teams:
|
||||
- <<: *radiant
|
||||
players:
|
||||
- {<<: *human1, index: 0}
|
||||
- {<<: *player, index: 1}
|
||||
- {<<: *player, index: 2}
|
||||
- {<<: *player, index: 3}
|
||||
- {<<: *player, index: 4}
|
||||
- <<: *dire
|
||||
players:
|
||||
- {<<: *human2, index: 0}
|
||||
- {<<: *player, index: 1}
|
||||
- {<<: *player, index: 2}
|
||||
- {<<: *player, index: 3}
|
||||
- {<<: *player, index: 4}
|
||||
|
||||
points:
|
||||
- {team: radiant, type: eagle, index: -1, direction: right, x: 0, y: 28}
|
||||
|
||||
Reference in New Issue
Block a user