[bot] fixes

This commit is contained in:
2018-01-18 22:29:50 +03:00
parent b9e3eba1fd
commit 5915f315ed
4 changed files with 22 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ class Bot implements EngineListener {
private var engine:Engine;
private var shotTimer:Timer;
private var turnTimer:Timer;
public function new(engine:Engine, tankId:Int) {
this.engine = engine;
@@ -34,6 +35,8 @@ class Bot implements EngineListener {
engine.action(tankId, TankAction.MOVE(Direction.BOTTOM));
shotTimer = new Timer(1000);
shotTimer.run = shot;
turnTimer = new Timer(3000);
turnTimer.run = turn;
}
public function shot():Void {
@@ -41,7 +44,16 @@ class Bot implements EngineListener {
}
public function turn():Void {
engine.action(tankId, TankAction.MOVE(Direction.TOP));
engine.action(tankId, TankAction.MOVE(randomDirection()));
}
private function randomDirection():Direction {
return [
Direction.TOP,
Direction.BOTTOM,
Direction.LEFT,
Direction.RIGHT,
][Math.floor(Math.random() * 4)];
}
public function dispose():Void {
@@ -49,6 +61,10 @@ class Bot implements EngineListener {
shotTimer.stop();
shotTimer = null;
}
if (turnTimer != null) {
turnTimer.stop();
turnTimer = null;
}
engine = null;
}
}

View File

@@ -91,6 +91,7 @@ class Engine {
}
public function action(tankId:Int, action:TankAction):Void {
if (!entities.exists(tankId)) return;
var tank:Tank = cast entities.get(tankId);
switch (action) {
case TankAction.MOVE(direction):
@@ -184,7 +185,7 @@ class Engine {
removedEntities.push(entity.key);
if (Std.is(entity, Bullet)) {
var tank:Tank = cast entities.get(cast(entity, Bullet).tankId);
tank.onDestroyBullet();
if (tank != null) tank.onDestroyBullet();
}
}
if (bots.exists(entity.id)) {