[bot] added
This commit is contained in:
@@ -12,14 +12,14 @@ import flash.Lib;
|
||||
class PlayerControl {
|
||||
|
||||
private var engine:Engine;
|
||||
private var id:Int;
|
||||
private var tankId:Int;
|
||||
|
||||
private var keyBinding:Map<Int, TankAction>;
|
||||
private var moveQueue:Array<Int>;
|
||||
private var shotTimer:Timer;
|
||||
|
||||
public function new(id:Int, engine:Engine, keyBinding:Map<Int, TankAction>) {
|
||||
this.id = id;
|
||||
public function new(tankId:Int, engine:Engine, keyBinding:Map<Int, TankAction>) {
|
||||
this.tankId = tankId;
|
||||
this.engine = engine;
|
||||
this.keyBinding = keyBinding;
|
||||
moveQueue = new Array<Int>();
|
||||
@@ -44,7 +44,7 @@ class PlayerControl {
|
||||
case _:
|
||||
}
|
||||
if (event.keyCode == Keyboard.U) {
|
||||
engine.action(id, TankAction.LEVEL_UP(1));
|
||||
engine.action(tankId, TankAction.LEVEL_UP(1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,18 +69,18 @@ class PlayerControl {
|
||||
|
||||
private function updateMove():Void {
|
||||
if (moveQueue.length == 0) {
|
||||
engine.action(id, TankAction.STOP);
|
||||
engine.action(tankId, TankAction.STOP);
|
||||
} else {
|
||||
switch (keyBinding.get(moveQueue[0])) {
|
||||
case TankAction.MOVE(direction):
|
||||
engine.action(id, TankAction.MOVE(direction));
|
||||
engine.action(tankId, TankAction.MOVE(direction));
|
||||
case _:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function shot():Void {
|
||||
engine.action(id, TankAction.SHOT);
|
||||
engine.action(tankId, TankAction.SHOT);
|
||||
}
|
||||
|
||||
public static function forPlayer(index:Int, tankId:Int, engine:Engine):PlayerControl {
|
||||
|
||||
@@ -38,7 +38,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
|
||||
content.addEventListener(Event.ENTER_FRAME, redraw);
|
||||
for (index in 0...game.players.length) {
|
||||
var playerId:Int = game.players[index].id;
|
||||
controls.set(playerId, PlayerControl.forPlayer(index, playerId, engine));
|
||||
controls.set(engine.playerTanks.get(playerId).id, PlayerControl.forPlayer(index, playerId, engine));
|
||||
}
|
||||
Provider.get(IConnection).packetHandler.addListener(this);
|
||||
render.draw(engine);
|
||||
|
||||
54
src/common/haxe/ru/m/tankz/bot/Bot.hx
Normal file
54
src/common/haxe/ru/m/tankz/bot/Bot.hx
Normal file
@@ -0,0 +1,54 @@
|
||||
package ru.m.tankz.bot;
|
||||
|
||||
import ru.m.geom.Direction;
|
||||
import haxe.Timer;
|
||||
import ru.m.tankz.map.Grid.GridCell;
|
||||
import ru.m.tankz.core.Tank;
|
||||
import Type.ValueType;
|
||||
import ru.m.tankz.core.Entity;
|
||||
import ru.m.tankz.engine.Engine;
|
||||
|
||||
class Bot implements EngineListener {
|
||||
|
||||
public var tankId(default, null):Int;
|
||||
private var engine:Engine;
|
||||
|
||||
private var shotTimer:Timer;
|
||||
|
||||
public function new(engine:Engine, tankId:Int) {
|
||||
this.engine = engine;
|
||||
this.tankId = tankId;
|
||||
}
|
||||
|
||||
public function onCollision(entity:Entity, with:Dynamic):Void {
|
||||
if (entity.id == tankId) {
|
||||
switch (Type.typeof(with)) {
|
||||
case ValueType.TClass(Tank): turn();
|
||||
case ValueType.TClass(GridCell): turn();
|
||||
case _:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function start():Void {
|
||||
engine.action(tankId, TankAction.MOVE(Direction.BOTTOM));
|
||||
shotTimer = new Timer(1000);
|
||||
shotTimer.run = shot;
|
||||
}
|
||||
|
||||
public function shot():Void {
|
||||
engine.action(tankId, TankAction.SHOT);
|
||||
}
|
||||
|
||||
public function turn():Void {
|
||||
engine.action(tankId, TankAction.MOVE(Direction.TOP));
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
if (shotTimer != null) {
|
||||
shotTimer.stop();
|
||||
shotTimer = null;
|
||||
}
|
||||
engine = null;
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,7 @@
|
||||
package ru.m.tankz.engine;
|
||||
|
||||
import haxe.macro.Type.Ref;
|
||||
import ru.m.tankz.bot.Bot;
|
||||
import haxe.Constraints.Function;
|
||||
import ru.m.geom.Rectangle;
|
||||
import ru.m.geom.Line;
|
||||
import ru.m.tankz.core.MobileEntity;
|
||||
import ru.m.tankz.core.Entity;
|
||||
@@ -27,13 +26,20 @@ class Engine {
|
||||
public var entities(default, null):Map<Int, Entity>;
|
||||
public var removedEntities(default, null):Array<String>;
|
||||
|
||||
private var playerTanks(default, null):Map<Int, Tank>;
|
||||
public var playerTanks(default, null):Map<Int, Tank>;
|
||||
private var time:Float;
|
||||
|
||||
public function new() {}
|
||||
public var listeners(default, null):Array<EngineListener>;
|
||||
|
||||
private var bots:Map<Int, Bot>;
|
||||
|
||||
public function new() {
|
||||
listeners = [];
|
||||
}
|
||||
|
||||
public function clear():Void {
|
||||
playerTanks = new Map<Int, Tank>();
|
||||
bots = new Map<Int, Bot>();
|
||||
}
|
||||
|
||||
private function buildTank(index:Int, config:TankConfig, point:SpawnPoint):Tank {
|
||||
@@ -47,6 +53,7 @@ class Engine {
|
||||
this.config = config;
|
||||
map = new LevelMap(config.map);
|
||||
playerTanks = new Map<Int, Tank>();
|
||||
bots = new Map<Int, Bot>();
|
||||
entities = new Map<Int, Entity>();
|
||||
removedEntities = new Array<String>();
|
||||
time = Date.now().getTime();
|
||||
@@ -75,12 +82,16 @@ class Engine {
|
||||
var point:SpawnPoint = config.getSpawnPoint(EntityType.BOT, index);
|
||||
var tank = buildTank(0, config.getTank('bot', 0), point);
|
||||
entities.set(tank.id, tank);
|
||||
var bot = new Bot(this, tank.id);
|
||||
bots.set(tank.id, bot);
|
||||
listeners.push(bot);
|
||||
bot.start();
|
||||
}
|
||||
return changes;
|
||||
}
|
||||
|
||||
public function action(playerId:Int, action:TankAction):Void {
|
||||
var tank:Tank = playerTanks.get(playerId);
|
||||
public function action(tankId:Int, action:TankAction):Void {
|
||||
var tank:Tank = cast entities.get(tankId);
|
||||
switch (action) {
|
||||
case TankAction.MOVE(direction):
|
||||
tank.move(direction);
|
||||
@@ -176,6 +187,12 @@ class Engine {
|
||||
tank.onDestroyBullet();
|
||||
}
|
||||
}
|
||||
if (bots.exists(entity.id)) {
|
||||
var bot:Bot = bots.get(entity.id);
|
||||
bot.dispose();
|
||||
listeners.remove(bot);
|
||||
bots.remove(entity.id);
|
||||
}
|
||||
}
|
||||
|
||||
public function update():Array<GameChange> {
|
||||
@@ -209,6 +226,7 @@ class Engine {
|
||||
if (cell.layer >= entity.layer && cell.layer < 3) {
|
||||
entity.rect.lean(cell.rect);
|
||||
collision = true;
|
||||
for (listener in listeners) listener.onCollision(ent, cell);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -220,7 +238,11 @@ class Engine {
|
||||
var funName = 'collision${ent.type}${other.type}';
|
||||
var fun:Function = Reflect.field(this, funName);
|
||||
if (fun != null) {
|
||||
collision = Reflect.callMethod(this, fun, [ent, other]) || collision;
|
||||
var c = Reflect.callMethod(this, fun, [ent, other]);
|
||||
if (c) {
|
||||
for (listener in listeners) listener.onCollision(ent, other);
|
||||
}
|
||||
collision = c || collision;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -256,3 +278,7 @@ class Engine {
|
||||
return changes;
|
||||
}
|
||||
}
|
||||
|
||||
interface EngineListener {
|
||||
public function onCollision(entity:Entity, with:Dynamic):Void;
|
||||
}
|
||||
Reference in New Issue
Block a user