[common] added control

This commit is contained in:
2018-01-20 12:47:05 +03:00
parent 5915f315ed
commit f1bb6514ad
6 changed files with 97 additions and 82 deletions

View File

@@ -1,38 +1,32 @@
package ru.m.tankz.bot;
import ru.m.tankz.control.Control;
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;
class Bot extends Control {
private var shotTimer:Timer;
private var turnTimer:Timer;
public function new(engine:Engine, tankId:Int) {
this.engine = engine;
this.tankId = tankId;
public function new(tankId:Int) {
super(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 _:
}
override public function onCollision(with:Dynamic):Void {
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));
action(TankAction.MOVE(Direction.BOTTOM));
shotTimer = new Timer(1000);
shotTimer.run = shot;
turnTimer = new Timer(3000);
@@ -40,11 +34,11 @@ class Bot implements EngineListener {
}
public function shot():Void {
engine.action(tankId, TankAction.SHOT);
action(TankAction.SHOT);
}
public function turn():Void {
engine.action(tankId, TankAction.MOVE(randomDirection()));
action(TankAction.MOVE(randomDirection()));
}
private function randomDirection():Direction {
@@ -56,7 +50,8 @@ class Bot implements EngineListener {
][Math.floor(Math.random() * 4)];
}
public function dispose():Void {
override public function dispose():Void {
super.dispose();
if (shotTimer != null) {
shotTimer.stop();
shotTimer = null;
@@ -65,6 +60,5 @@ class Bot implements EngineListener {
turnTimer.stop();
turnTimer = null;
}
engine = null;
}
}

View File

@@ -0,0 +1,44 @@
package ru.m.tankz.control;
import ru.m.geom.Direction;
enum TankAction {
MOVE(direction:Direction);
LEVEL_UP(level:Int);
STOP;
SHOT;
}
class Control {
public var tankId(default, null):Int;
private var listener:ControlListener;
public function new(tankId:Int) {
this.tankId = tankId;
}
public function bind(listener:ControlListener):Void {
this.listener = listener;
}
public function action(action:TankAction):Void {
if (listener != null) {
listener.onAction(tankId, action);
}
}
public function onCollision(with:Dynamic):Void {
}
public function dispose():Void {
listener = null;
}
}
interface ControlListener {
public function onAction(tankId:Int, action:TankAction):Void;
}

View File

@@ -1,19 +1,12 @@
package ru.m.tankz.core;
import ru.m.geom.Point;
import ru.m.tankz.config.Config.TankConfig;
import ru.m.tankz.config.Config;
import ru.m.tankz.core.Bullet;
import ru.m.geom.Rectangle;
import ru.m.geom.Direction;
enum TankAction {
MOVE(direction:Direction);
LEVEL_UP(level:Int);
STOP;
SHOT;
}
class Tank extends MobileEntity {
public var index(default, null):Int;
public var config(default, set):TankConfig;
@@ -45,15 +38,6 @@ class Tank extends MobileEntity {
return bullet;
}
override public function move(direction:Direction):Void {
// ToDo: spike
/*if (direction != this.direction) {
rect.x -= this.direction.x * 4;
rect.y -= this.direction.y * 4;
}*/
super.move(direction);
}
public function onDestroyBullet():Void {
bulletsCounter--;
}

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.engine;
import ru.m.tankz.control.Control;
import ru.m.tankz.bot.Bot;
import haxe.Constraints.Function;
import ru.m.geom.Line;
@@ -18,7 +19,7 @@ import ru.m.tankz.core.Tank;
import ru.m.tankz.map.LevelMap;
class Engine {
class Engine implements ControlListener {
public var config(default, default):Config;
public var map(default, null):LevelMap;
@@ -29,17 +30,13 @@ class Engine {
public var playerTanks(default, null):Map<Int, Tank>;
private var time:Float;
public var listeners(default, null):Array<EngineListener>;
private var controls:Map<Int, Control>;
private var bots:Map<Int, Bot>;
public function new() {
listeners = [];
}
public function new() {}
public function clear():Void {
playerTanks = new Map<Int, Tank>();
bots = new Map<Int, Bot>();
controls = new Map<Int, Control>();
}
private function buildTank(index:Int, config:TankConfig, point:SpawnPoint):Tank {
@@ -53,7 +50,7 @@ class Engine {
this.config = config;
map = new LevelMap(config.map);
playerTanks = new Map<Int, Tank>();
bots = new Map<Int, Bot>();
controls = new Map<Int, Control>();
entities = new Map<Int, Entity>();
removedEntities = new Array<String>();
time = Date.now().getTime();
@@ -82,15 +79,19 @@ 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);
var bot = new Bot(tank.id);
bindControl(bot);
bot.start();
}
return changes;
}
public function action(tankId:Int, action:TankAction):Void {
public function bindControl(control:Control):Void {
control.bind(this);
controls.set(control.tankId, control);
}
public function onAction(tankId:Int, action:TankAction):Void {
if (!entities.exists(tankId)) return;
var tank:Tank = cast entities.get(tankId);
switch (action) {
@@ -188,11 +189,10 @@ class Engine {
if (tank != null) tank.onDestroyBullet();
}
}
if (bots.exists(entity.id)) {
var bot:Bot = bots.get(entity.id);
bot.dispose();
listeners.remove(bot);
bots.remove(entity.id);
if (controls.exists(entity.id)) {
var control:Control = controls.get(entity.id);
control.dispose();
controls.remove(entity.id);
}
}
@@ -227,7 +227,9 @@ 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);
if (controls.exists(entity.id)) {
controls.get(entity.id).onCollision(cell);
}
break;
}
}
@@ -241,7 +243,9 @@ class Engine {
if (fun != null) {
var c = Reflect.callMethod(this, fun, [ent, other]);
if (c) {
for (listener in listeners) listener.onCollision(ent, other);
if (controls.exists(entity.id)) {
controls.get(entity.id).onCollision(other);
}
}
collision = c || collision;
}
@@ -279,7 +283,3 @@ class Engine {
return changes;
}
}
interface EngineListener {
public function onCollision(entity:Entity, with:Dynamic):Void;
}