[common] split player and control
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz.view.frames;
|
||||
|
||||
import ru.m.tankz.control.PlayerControl;
|
||||
import flash.events.Event;
|
||||
import haxe.Timer;
|
||||
import haxework.gui.VGroupView;
|
||||
@@ -32,6 +33,8 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
|
||||
humans: 1,
|
||||
bots: 3,
|
||||
});
|
||||
game.setControl('player', 0, PlayerControl.forPlayer(0));
|
||||
//game.setControl('player', 1, PlayerControl.forPlayer(1));
|
||||
content.addEventListener(Event.ENTER_FRAME, redraw);
|
||||
Provider.get(IConnection).packetHandler.addListener(this);
|
||||
render.draw(game.engine);
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package ru.m.tankz.bot;
|
||||
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.game.Player;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.geom.Direction;
|
||||
import haxe.Timer;
|
||||
@@ -10,13 +8,13 @@ import ru.m.tankz.core.Tank;
|
||||
import Type.ValueType;
|
||||
|
||||
|
||||
class Bot extends Player {
|
||||
class Bot extends Control {
|
||||
|
||||
private var shotTimer:Timer;
|
||||
private var turnTimer:Timer;
|
||||
|
||||
public function new(team:TeamId, index:Int) {
|
||||
super(team, index);
|
||||
public function new() {
|
||||
super();
|
||||
}
|
||||
|
||||
override public function onCollision(with:Dynamic):Void {
|
||||
@@ -27,12 +25,27 @@ class Bot extends Player {
|
||||
}
|
||||
}
|
||||
|
||||
public function start():Void {
|
||||
override public function start():Void {
|
||||
action(TankAction.MOVE(Direction.BOTTOM));
|
||||
shotTimer = new Timer(1000);
|
||||
shotTimer.run = shot;
|
||||
turnTimer = new Timer(3000);
|
||||
turnTimer.run = turn;
|
||||
if (shotTimer == null) {
|
||||
shotTimer = new Timer(1000);
|
||||
shotTimer.run = shot;
|
||||
}
|
||||
if (turnTimer == null) {
|
||||
turnTimer = new Timer(3000);
|
||||
turnTimer.run = turn;
|
||||
}
|
||||
}
|
||||
|
||||
override public function stop():Void {
|
||||
if (shotTimer != null) {
|
||||
shotTimer.stop();
|
||||
shotTimer = null;
|
||||
}
|
||||
if (turnTimer != null) {
|
||||
turnTimer.stop();
|
||||
turnTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
public function shot():Void {
|
||||
@@ -51,16 +64,4 @@ class Bot extends Player {
|
||||
Direction.RIGHT,
|
||||
][Math.floor(Math.random() * 4)];
|
||||
}
|
||||
|
||||
override public function dispose():Void {
|
||||
super.dispose();
|
||||
if (shotTimer != null) {
|
||||
shotTimer.stop();
|
||||
shotTimer = null;
|
||||
}
|
||||
if (turnTimer != null) {
|
||||
turnTimer.stop();
|
||||
turnTimer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,8 +31,12 @@ class Control {
|
||||
|
||||
}
|
||||
|
||||
public function start():Void {}
|
||||
|
||||
public function stop():Void {}
|
||||
|
||||
public function dispose():Void {
|
||||
stop();
|
||||
listener = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.bot.Bot;
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.config.Config;
|
||||
@@ -24,13 +25,12 @@ class ClassicGame extends Game<ClassicGameSettings> {
|
||||
var humans = new Team('player', settings.humans, 3);
|
||||
for (index in 0...humans.size) {
|
||||
var player = new Player(humans.id, index);
|
||||
player.bind(this);
|
||||
humans.players.push(player);
|
||||
}
|
||||
var bots = new Team('bot', settings.bots, 20);
|
||||
for (index in 0...bots.size) {
|
||||
var bot = new Bot(bots.id, 0);
|
||||
bot.bind(this);
|
||||
var bot = new Player(bots.id, 0, new Bot());
|
||||
bot.control.bind(this);
|
||||
bots.players.push(bot);
|
||||
}
|
||||
teams = new Map<TeamId, Team>();
|
||||
@@ -50,7 +50,6 @@ class ClassicGame extends Game<ClassicGameSettings> {
|
||||
var tank = buildTank(0, bots.id, config.getTank(bots.type, 0), point);
|
||||
engine.spawn(tank);
|
||||
player.tankId = tank.id;
|
||||
cast(player, Bot).start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,13 +15,13 @@ typedef TeamId = Int;
|
||||
typedef GameSettings = {}
|
||||
|
||||
|
||||
class Game<G:GameSettings> implements EngineListener implements ControlListener {
|
||||
class Game<S:GameSettings> implements EngineListener implements ControlListener {
|
||||
|
||||
public var type(default, null):GameType;
|
||||
public var teams(default, null):Map<TeamId, Team>;
|
||||
public var config(default, null):Config;
|
||||
public var engine(default, null):Engine;
|
||||
public var settings(default, null):G;
|
||||
public var settings(default, null):S;
|
||||
|
||||
|
||||
public function new(type:GameType, config:Config) {
|
||||
@@ -37,10 +37,26 @@ class Game<G:GameSettings> implements EngineListener implements ControlListener
|
||||
return tank;
|
||||
}
|
||||
|
||||
public function start(settings:G):Void {
|
||||
public function start(settings:S):Void {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
public function setControl(teamType:String, index:Int, control:Control):Void {
|
||||
for (team in teams.iterator()) {
|
||||
if (team.type == teamType) {
|
||||
L.w('XXX', 'players: ${team.players}');
|
||||
var player = team.players[index];
|
||||
if (player.control != null) {
|
||||
player.control.dispose();
|
||||
}
|
||||
player.control = control;
|
||||
player.control.bind(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function onSpawn(entity:EntityType):Void {
|
||||
|
||||
}
|
||||
@@ -53,10 +69,12 @@ class Game<G:GameSettings> implements EngineListener implements ControlListener
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function onAction(tankId:Int, action:TankAction):Void {
|
||||
engine.action(tankId, action);
|
||||
}
|
||||
|
||||
|
||||
public function dispose():Void {
|
||||
|
||||
}
|
||||
|
||||
@@ -1,17 +1,44 @@
|
||||
package ru.m.tankz.game;
|
||||
|
||||
import ru.m.tankz.game.Game;
|
||||
import ru.m.tankz.control.Control;
|
||||
import ru.m.tankz.game.Game;
|
||||
|
||||
|
||||
class Player extends Control {
|
||||
class Player {
|
||||
|
||||
public var index(default, null):Int;
|
||||
public var team(default, null):TeamId;
|
||||
public var tankId(default, set):Int;
|
||||
public var control(default, set):Control;
|
||||
|
||||
public function new(team:TeamId, index:Int) {
|
||||
super();
|
||||
public function new(team:TeamId, index:Int, control:Control=null) {
|
||||
this.team = team;
|
||||
this.index = index;
|
||||
this.control = control;
|
||||
}
|
||||
|
||||
public function set_tankId(value:Int):Int {
|
||||
tankId = value;
|
||||
if (control != null) {
|
||||
control.tankId = tankId;
|
||||
if (tankId != 0) {
|
||||
control.start();
|
||||
} else {
|
||||
control.stop();
|
||||
}
|
||||
}
|
||||
return tankId;
|
||||
}
|
||||
|
||||
public function set_control(value:Control):Control {
|
||||
if (control != null) control.dispose();
|
||||
control = value;
|
||||
if (control != null) {
|
||||
control.tankId = tankId;
|
||||
if (tankId != 0) {
|
||||
control.start();
|
||||
}
|
||||
}
|
||||
return control;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user