[bot] added

This commit is contained in:
2018-01-18 15:58:18 +03:00
parent 507320414f
commit b9e3eba1fd
4 changed files with 95 additions and 15 deletions

View 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;
}
}

View File

@@ -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;
}