[bot] fixes
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user