[common] humans in DotaGame

This commit is contained in:
2018-02-04 21:02:08 +03:00
parent e0ceff68f9
commit 1c9ccf0fb8
12 changed files with 136 additions and 97 deletions

View File

@@ -7,14 +7,13 @@ import haxe.Timer;
class BotControl extends Control {
public static var TYPE(default, never):ControlType = 'bot';
private var shotTimer:Timer;
private var turnRandomTimer:Timer;
private var turnTimer:Timer;
public function new(index:Int) {
super({type:TYPE, index:index});
super({type:Control.BOT, index:index});
}
override public function onCollision(with:EntityType):Void {

View File

@@ -23,6 +23,10 @@ typedef ControlId = {
class Control {
public static var NONE(default, never):ControlType = 'none';
public static var HUMAN(default, never):ControlType = 'human';
public static var BOT(default, never):ControlType = 'bot';
public var id:ControlId;
public var tankId(default, default):Int;
private var handler:ControlHandler;

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game;
import ru.m.tankz.control.Control;
import haxe.ds.Option;
import ru.m.tankz.game.GameState.PlayerState;
import ru.m.tankz.game.Game;
@@ -12,6 +13,9 @@ class ClassicGame extends Game {
public static var HUMAN(default, never):TeamId = 'human';
public static var BOT(default, never):TeamId = 'bot';
public static var PLAYER1(default, never):GameMode = [{team:HUMAN, type:Control.HUMAN, index:0}];
public static var PLAYER2(default, never):GameMode = [{team:HUMAN, type:Control.HUMAN, index:0}, {team:HUMAN, type:Control.HUMAN, index:1}];
private static var HUMAN_LIFE(default, never):Int = 3;
private static var BOT_LIFE(default, never):Int = 20;
@@ -19,37 +23,22 @@ class ClassicGame extends Game {
super(TYPE);
}
public static function buildState(level:Int, humans:Int):GameState {
public static function buildState(level:Int, mode:GameMode):GameState {
var state = new GameState();
state.type = TYPE;
state.mode = mode;
state.level = level;
state.teams[HUMAN] = {life: -1, players: new Map(), lose: false};
state.teams[BOT] = {life: BOT_LIFE, players: new Map(), lose: false};
for (i in 0...humans) {
state.teams[HUMAN].players[i] = {
index:i,
tank:{
group: HUMAN,
type: '1'
},
control:{
type: 'human',
index: i
},
for (human in mode) {
state.teams[HUMAN].players[human.index] = {
id:human,
life:HUMAN_LIFE,
};
}
for (i in 0...humans*2+2) {
for (i in 0...mode.length * 2 + 2) {
state.teams[BOT].players[i] = {
index:i,
tank:{
group: BOT,
type: '1'
},
control:{
type: 'bot',
index: i
},
id:{team:BOT, index:i, type:Control.BOT},
life:-1,
};
}

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game;
import ru.m.tankz.control.Control;
import haxe.ds.Option;
import ru.m.tankz.game.Game;
import ru.m.tankz.game.GameState;
@@ -12,52 +13,54 @@ class DotaGame extends Game {
public static var RADIANT(default, never):TeamId = 'radiant';
public static var DIRE(default, never):TeamId = 'dire';
public static var PLAYER1(default, never):GameMode = [
{team:RADIANT, type:Control.HUMAN, index:0}
];
public static var PLAYER2_COOP(default, never):GameMode = [
{team:RADIANT, type:Control.HUMAN, index:0},
{team:RADIANT, type:Control.HUMAN, index:1}
];
public static var PLAYER2_VS(default, never):GameMode = [
{team:RADIANT, type:Control.HUMAN, index:0},
{team:DIRE, type:Control.HUMAN, index:0}
];
private static var TEAM_SIZE(default, never):Int = 5;
public function new() {
super(TYPE);
}
public static function buildState(level:Int, humans:Int):GameState {
public static function buildState(level:Int, mode:GameMode):GameState {
var state = new GameState();
state.type = TYPE;
state.mode = mode;
state.level = level;
state.teams[RADIANT] = {life: 20, players: new Map(), lose: false};
state.teams[DIRE] = {life: 20, players: new Map(), lose: false};
state.teams[DIRE] = {life: 20, players: new Map(), lose: false};
for (i in 0...TEAM_SIZE) {
state.teams[RADIANT].players[i] = {
index:i,
tank:{
group: RADIANT,
type: '1'
},
control:{
type: 'bot',
index: i
},
life:-1,
id: {team:RADIANT, index:i, type:Control.BOT},
life: -1,
};
}
for (i in 0...TEAM_SIZE) {
state.teams[DIRE].players[i] = {
index:i,
tank:{
group: DIRE,
type: '1'
},
control:{
type: 'bot',
index: i
},
life:-1,
id: {team:DIRE, index:i, type:Control.BOT},
life: -1,
};
}
for (human in mode) {
state.teams[human.team].players[human.index].id = human;
}
return state;
}
override public function next():Option<GameState> {
state.level++;
if (state.level >= config.game.levels) state.level = 0;
return Option.Some(buildState(state.level, 0));
return Option.Some(buildState(state.level, state.mode));
}
}

View File

@@ -23,10 +23,13 @@ import ru.m.tankz.game.Spawner;
typedef GameType = String;
typedef GameMode = Array<PlayerId>;
typedef TeamId = String;
typedef PlayerId = {
var team:TeamId;
var type:ControlType;
var index:Int;
}
@@ -75,18 +78,19 @@ class Game implements EngineListener {
engine.map.setData(bricks);
teams = new Map<TeamId, Team>();
spawners = new Map<TeamId, Spawner>();
var humanControlIndex = 0;
for (teamConfig in config.teams) {
var team = new Team(teamConfig);
for (playerState in state.teams.get(team.id).players) {
var player = new Player({team:team.id, index:playerState.index});
var player = new Player(playerState.id);
team.players.push(player);
teams.set(team.id, team);
if (playerState.control != null) {
var control = switch (playerState.control.type) {
case HumanControl.TYPE: new HumanControl(playerState.control.index);
case BotControl.TYPE: new BotControl(playerState.control.index);
case 'none': null;
case _: throw 'Unsupported control type: "${playerState.control.type}"';
if (player.id.type != null) {
var control = switch (player.id.type) {
case Control.HUMAN: new HumanControl(humanControlIndex++);
case Control.BOT: new BotControl(player.id.index);
case Control.NONE: null;
case _: throw 'Unsupported control type: "${player.id.type}"';
}
if (control != null) {
player.control = control;

View File

@@ -1,23 +1,14 @@
package ru.m.tankz.game;
import ru.m.tankz.game.Game;
import ru.m.tankz.config.Config;
typedef ControlType = String;
typedef ControId = {
var type:ControlType;
var index:Int;
}
typedef PlayerState = {
var index:Int;
var tank:TankType;
var id:PlayerId;
var life:Int;
var control:ControId;
}
@@ -30,6 +21,7 @@ typedef TeamState = {
class GameState {
public var type:GameType;
public var mode:GameMode;
public var level:Int;
public var teams:Map<TeamId, TeamState>;