[client] add RecordFrame

This commit is contained in:
2019-05-07 17:21:01 +03:00
parent a7f286dc42
commit 183c6c16f1
19 changed files with 162 additions and 63 deletions

View File

@@ -152,8 +152,9 @@ import ru.m.tankz.map.LevelMap;
}
public function dispose():Void {
entities = null;
map = null;
// ToDo: set to null
entities = new Map();
//map = null;
spawnSignal.dispose();
collisionSignal.dispose();
moveSignal.dispose();

View File

@@ -29,13 +29,25 @@ import ru.m.tankz.Type;
@:provide var configBundle:IConfigBundle;
public function new(type:GameType) {
this.type = type;
public function new(state:GameState) {
this.type = state.type;
this.teams = new Map();
this.config = configBundle.get(type);
this.engine = new Engine(config);
this.builder = new EntityBuilder(config);
connect(this);
prepare(state);
}
private function prepare(state:GameState):Void {
var level:LevelConfig = state.level;
var points:Array<SpawnPoint> = level.points != null ? level.points : config.points;
engine.map.setData(level.data);
for (teamConfig in state.preset.teams) {
var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id);
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);
teams[team.id] = team;
}
}
private function applyPoint(entity:Entity, point:SpawnPoint):Void {

View File

@@ -30,10 +30,10 @@ enum DestroyEvent {
}
enum ChangeEvent {
PLAYER_SCORE(player:Player, value:Int);
PLAYER_LIFE(player:Player, value:Int);
TEAM_SCORE(team:Team, value:Int);
TEAM_LIFE(team:Team, value:Int);
PLAYER_SCORE(playerId:PlayerId, value:Int);
PLAYER_LIFE(playerId:PlayerId, value:Int);
TEAM_SCORE(teamId:TeamId, value:Int);
TEAM_LIFE(teamId:TeamId, value:Int);
}
enum GameEvent {

View File

@@ -23,7 +23,6 @@ class GameRunner implements EngineListener implements GameListener {
private var game(default, null):IGame;
private var gameEventSignal(get, null):Signal<GameEvent>;
private var points:Array<SpawnPoint>;
private var timer:Timer;
@@ -52,13 +51,7 @@ class GameRunner implements EngineListener implements GameListener {
}
public function start(state:GameState):Void {
var level:LevelConfig = state.level;
points = level.points != null ? level.points : game.config.points;
game.engine.map.setData(level.data);
for (teamConfig in state.preset.teams) {
var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id);
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);
game.teams[team.id] = team;
for (team in game.teams.iterator()) {
for (player in team.players.iterator()) {
var control = controlFactory.build(player.id, AController.fromString(player.config.control));
if (control != null) {
@@ -67,8 +60,6 @@ class GameRunner implements EngineListener implements GameListener {
}
}
team.spawner.runner = spawn;
}
for (team in game.teams.iterator()) {
for (player in team.players.iterator()) {
if (team.tryRespawn(player.id)) {
team.spawner.push(player.id);
@@ -250,20 +241,20 @@ class GameRunner implements EngineListener implements GameListener {
var player = game.getPlayer(playerId);
var team = game.getTeam(playerId.team);
player.state.score += score;
gameEventSignal.emit(GameEvent.CHANGE(PLAYER_SCORE(player, player.state.score)));
gameEventSignal.emit(GameEvent.CHANGE(TEAM_SCORE(team, game.state.getTeamScore(team.id))));
gameEventSignal.emit(GameEvent.CHANGE(PLAYER_SCORE(playerId, player.state.score)));
gameEventSignal.emit(GameEvent.CHANGE(TEAM_SCORE(playerId.team, game.state.getTeamScore(team.id))));
}
private function changeLife(playerId:PlayerId, life:Int):Void {
var player = game.getPlayer(playerId);
player.state.life += life;
gameEventSignal.emit(GameEvent.CHANGE(PLAYER_LIFE(player, player.state.life)));
gameEventSignal.emit(GameEvent.CHANGE(PLAYER_LIFE(playerId, player.state.life)));
}
private function changeTeamLife(teamId:TeamId, life:Int):Void {
var team = game.getTeam(teamId);
team.state.life += life;
gameEventSignal.emit(GameEvent.CHANGE(TEAM_LIFE(team, team.state.life)));
gameEventSignal.emit(GameEvent.CHANGE(TEAM_LIFE(teamId, team.state.life)));
}
public function onGameEvent(event:GameEvent):Void {

View File

@@ -18,13 +18,6 @@ class GamePlayer {
this.data = null;
}
public function onGameEvent(event:GameEvent):Void {
switch event {
case GameEvent.COMPLETE(_, _):
stop();
}
}
public function start():Void {
frame = 0;
data = record.events.slice(0);
@@ -42,15 +35,16 @@ class GamePlayer {
if (event.frame <= frame) {
events++;
game.gameEventSignal.emit(event.event);
switch event {
switch event.event {
case GameEvent.COMPLETE(_, _):
stop();
case _:
}
} else {
break;
}
}
if (event > 0) {
if (events > 0) {
data = data.slice(events);
}
}

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game.record;
import ru.m.tankz.Type;
import com.hurlant.crypto.extra.UUID;
import com.hurlant.crypto.prng.Random;
@@ -11,11 +12,24 @@ typedef EventItem = {
class GameRecord {
public var id(default, default):String;
public var date(default, default):Date;
public var type(default, default):GameType;
public var presetId(default, default):PresetId;
public var levelId(default, default):LevelId;
public var events(default, default):Array<EventItem>;
public var state(get, null):GameState;
public function new() {
this.id = UUID.generateRandom(new Random()).toString();
this.date = null;
this.events = [];
}
private inline function get_state():GameState {
return new GameState(type, presetId, levelId);
}
public function toString():String {
return 'GameRecord{id=$id,date=$date,type=$type,preset=$presetId,level=$levelId,events=${events.length}}';
}
}

View File

@@ -16,7 +16,10 @@ class GameRecorder implements GameListener {
public function onGameEvent(event:GameEvent):Void {
switch event {
case GameEvent.START(_):
case GameEvent.START(state):
record.type = state.type;
record.presetId = state.presetId;
record.levelId = state.levelId;
start();
case GameEvent.COMPLETE(_, _):
stop();

View File

@@ -1,19 +1,13 @@
package ru.m.tankz.network;
import ru.m.tankz.proto.game.GameChangeProto;
import ru.m.tankz.proto.core.GameProto;
import ru.m.tankz.Type;
import ru.m.tankz.game.Game;
import ru.m.tankz.proto.core.GameProto;
import ru.m.tankz.proto.game.GameChangeProto;
class NetworkGame extends Game {
private static var TAG(default, never):String = 'NetworkGame';
public function new(type:GameType) {
super(type);
}
public function load(proto:GameProto):Void {
L.w(TAG, 'load: ${proto}');
// ToDo:

View File

@@ -163,7 +163,7 @@ presets:
- {<<: *team_human}
- id: bot
spawnInterval: 3000
life: 10
life: 1
players:
- {<<: *bot, index: 0, control: bot-stupid}
- {<<: *bot, index: 1, control: bot-stupid}