[common] PlayerState update

This commit is contained in:
2019-03-28 17:38:51 +03:00
parent d7b7744eb8
commit f60feea6e8
4 changed files with 52 additions and 32 deletions

View File

@@ -2,7 +2,6 @@ package ru.m.tankz.game;
import haxe.ds.Option;
import haxe.Timer;
import haxework.color.Color;
import haxework.provider.Provider;
import haxework.signal.Signal;
import ru.m.geom.Point;
@@ -120,17 +119,10 @@ class Game extends GameDispatcher {
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);
teams[team.id] = team;
for (player in team.players.iterator()) {
var controller:Controller = Controller.BOT("hard");
var playerState = state.players[player.id];
if (playerState != null) {
player.state = playerState;
switch (playerState.controller) {
case HUMAN(_):
controller = playerState.controller;
case _:
}
if (player.state.controller == NONE) {
player.state.controller = BOT("hard");
}
var control = controlFactory.build(player.id, controller);
var control = controlFactory.build(player.id, player.state.controller);
L.d(TAG, 'control(${player.id} - ${control})');
if (control != null) {
player.control = control;

View File

@@ -6,38 +6,63 @@ import ru.m.tankz.config.Config;
import ru.m.tankz.control.Control;
import ru.m.tankz.Type;
class PlayerState {
public var id:PlayerId;
public var tank:TankType;
public var controller:Controller;
public var color:Color;
public var life:Int;
class State {
public var score:Int;
public var frags:Int;
public var shots:Int;
public var hits:Int;
public function new(id:PlayerId, controller:Controller = null, life:Int = 0) {
public function new() {
}
public function add(state:State) {
score += state.score;
frags += state.frags;
shots += state.shots;
hits += state.hits;
}
public function reset() {
score = 0;
frags = 0;
shots = 0;
hits = 0;
}
}
class PlayerState extends State {
public var id:PlayerId;
public var tank:TankType;
public var controller:Controller;
public var color:Color;
public var life:Int;
public var total:State;
public function new(id:PlayerId, controller:Controller = null) {
super();
this.id = id;
this.tank = null;
this.controller = controller == null ? Controller.NONE : controller;
this.life = life;
this.score = 0;
this.frags = 0;
this.shots = 0;
this.hits = 0;
this.life = 0;
this.total = new State();
}
override public function reset() {
total.add(this);
super.reset();
}
}
class TeamState {
public var id:TeamId;
public var players:Array<PlayerState>;
public var players:Map<Int, PlayerState>;
public var life:Int;
public function new(id:TeamId, life:Int = 0) {
public function new(id:TeamId) {
this.id = id;
this.life = life;
this.players = [];
this.life = 0;
this.players = new Map();
}
}
@@ -61,13 +86,13 @@ class GameState {
this.teams = new Map();
this.players = new Map();
for (team in preset.teams) {
var teamState = new TeamState(team.id, team.life);
var teamState = new TeamState(team.id);
for (player in team.players) {
var playerId = new PlayerId(team.id, player.index);
var controller = player.human ? HUMAN(player.index) : NONE;
var playerState = new PlayerState(playerId, controller, player.life);
var playerState = new PlayerState(playerId, controller);
players[playerId] = playerState;
teamState.players.push(playerState);
teamState.players[player.index] = playerState;
}
teams[team.id] = teamState;
}
@@ -117,6 +142,6 @@ class GameState {
public function getPlayerColor(id:PlayerId):Color {
var playerState = players[id];
return playerState == null || playerState.color.zero ? config.getColor(id) : playerState.color;
return (playerState == null || playerState.color.zero) ? config.getColor(id) : playerState.color;
}
}

View File

@@ -18,6 +18,8 @@ class Player {
this.id = new PlayerId(teamId, config.index);
this.control = null;
this.state = state == null ? new PlayerState(id) : state;
this.state.reset();
this.state.life = config.life;
}
private function set_tankId(value:Int):Int {

View File

@@ -21,7 +21,8 @@ class Team {
this.config = config;
this.players = new Map();
for (playerConfig in config.players) {
var player:Player = new Player(id, playerConfig);
var playerState = state == null ? null : state.players[playerConfig.index];
var player:Player = new Player(id, playerConfig, playerState);
this.players[playerConfig.index] = player;
}
this.spawner = new Spawner(config, points);