[common] multiple weapon support

This commit is contained in:
2019-08-13 16:41:57 +03:00
parent 314f5a480c
commit 48f9c433e2
19 changed files with 120 additions and 86 deletions

View File

@@ -13,6 +13,10 @@ class Point {
return new Point(x + point.x, y + point.y);
}
public function clone():Point {
return new Point(x, y);
}
public function hashCode():Int {
return Std.int(x + 1000 * y);
}

View File

@@ -32,7 +32,7 @@ class BotControl extends Control {
shotTimer.stop();
shotTimer = null;
}
action(SHOT);
action(SHOT(0));
}
public function shot(delay:Int = 100):Void {

View File

@@ -10,7 +10,7 @@ import ru.m.tankz.Type;
enum TankAction {
MOVE(direction:Direction);
STOP;
SHOT;
SHOT(weapon:Int);
}
class Control {

View File

@@ -9,6 +9,7 @@ class Bullet extends MobileEntity {
public var playerId(default, null):PlayerId;
public var tankId(get, null):Int;
public var tank(default, default):Tank;
public var weapon(default, default):Weapon;
public var config(default, null):BulletConfig;
public function new(id:Int, rect:Rectangle, playerId:PlayerId, config:BulletConfig) {

View File

@@ -5,8 +5,10 @@ import ru.m.tankz.config.Config;
class Weapon {
public var config(default, null):WeaponConfig;
public var count(default, default):Int;
public function new(config:WeaponConfig) {
this.config = config;
this.count = 0;
}
}

View File

@@ -56,9 +56,7 @@ class EntityBuilder {
return tank;
}
public function buildBullet(point:Point, direction:Direction, playerId:PlayerId, type:TankType):Bullet {
var tankConfig = config.getTank(type);
var bulletConfig = tankConfig.weapons[0].bullet;
public function buildBullet(point:Point, direction:Direction, playerId:PlayerId, bulletConfig:BulletConfig):Bullet {
var bullet = new Bullet(++entityId, new Rectangle(point.x - bulletConfig.width / 2, point.y - bulletConfig.height / 2, bulletConfig.width, bulletConfig.height, direction), playerId, bulletConfig);
return bullet;
}

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game;
import ru.m.tankz.core.Weapon;
import ru.m.geom.Line;
import ru.m.geom.Point;
import ru.m.tankz.bonus.BonusFactory;
@@ -155,8 +156,10 @@ class GameRunner extends Game implements EngineListener {
tank.rect.lean(cell.rect);
emitTankMove(tank);
case [BULLET(bullet), BULLET(other_bullet)]:
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
gameEventSignal.emit(DESTROY(BULLET(other_bullet.id)));
if (bullet.playerId != other_bullet.playerId) {
gameEventSignal.emit(DESTROY(BULLET(bullet.id)));
gameEventSignal.emit(DESTROY(BULLET(other_bullet.id)));
}
case [BULLET(bullet), CELL(cell)]:
bullet.rect.lean(cell.rect);
gameEventSignal.emit(HIT(CELL(cell.cellX, cell.cellY, buildShot(bullet))));
@@ -256,14 +259,16 @@ class GameRunner extends Game implements EngineListener {
timer.stop();
timer = null;
}
case ACTION(tankId, SHOT):
case ACTION(tankId, SHOT(index)):
var tank:Tank = cast engine.entities.get(tankId);
var player = getPlayer(tank.playerId);
if (!tank.freezing && player.bullets < tank.weapon.config.count) {
var weapon:Weapon = tank.weapons.length > index - 1 ? tank.weapons[index] : null;
if (weapon != null && !tank.freezing && weapon.count < weapon.config.count) {
var rect = tank.rect;
var point = rect.center.add(new Point(rect.width / 4 * rect.direction.x, rect.height / 4 * rect.direction.y));
var bullet = builder.buildBullet(point, rect.direction, player.id, tank.config.type);
var bullet = builder.buildBullet(point, rect.direction, player.id, weapon.config.bullet);
bullet.tank = tank;
bullet.weapon = weapon;
bullet.move(bullet.rect.direction);
engine.spawn(bullet);
gameEventSignal.emit(EventUtil.buildBulletSpawn(bullet));
@@ -279,8 +284,9 @@ class GameRunner extends Game implements EngineListener {
var player = getPlayer(playerId);
player.tankId = id;
player.state.tank = info;
case SPAWN(BULLET(_, _, playerId, _)):
getPlayer(playerId).bullets++;
case SPAWN(BULLET(id, _, _, _)):
var bullet:Bullet = engine.getEntity(id);
bullet.weapon.count++;
case CHANGE(BRICK(id, type)):
engine.map.bricksById[id].config = config.getBrick(type);
case DESTROY(EAGLE(id, shot)):
@@ -344,8 +350,7 @@ class GameRunner extends Game implements EngineListener {
engine.destroy(id);
case DESTROY(BULLET(id)):
var bullet:Bullet = engine.getEntity(id);
var player = getPlayer(bullet.playerId);
player.bullets--;
bullet.weapon.count--;
var side:Line = bullet.rect.getSide(bullet.rect.direction.reverse()).move(
// ToDo: move
new Point(bullet.rect.direction.x * 5, bullet.rect.direction.y * 5)

View File

@@ -10,7 +10,6 @@ class Player {
public var tankId(default, set):Int;
public var isAlive(get, null):Bool;
public var state(default, default):PlayerState;
public var bullets(default, default):Int;
public function new(teamId:TeamId, config:PlayerConfig, state:PlayerState = null) {
this.config = config;
@@ -18,7 +17,6 @@ class Player {
this.state = state == null ? new PlayerState(id) : state;
this.state.reset();
this.state.life = Math.isNaN(config.life) ? 0 : config.life;
this.bullets = 0;
this.tankId = -1;
}

View File

@@ -86,5 +86,7 @@ tanks:
weapons:
- bullet: {<<: *bullet, speed: 12.0}
count: 2
- bullet: {<<: *bullet, speed: 4.0, width: 16, height: 16, piercing: 4}
count: 1
bonuses: []