[common] bonus config duration

This commit is contained in:
2018-02-16 22:19:14 +03:00
parent a93a67161d
commit 5456a01551
10 changed files with 49 additions and 24 deletions

View File

@@ -125,6 +125,8 @@ class Render extends SpriteView implements EngineListener {
}
}
public function onChange(entity:EntityType):Void {}
public function onCollision(entity:EntityType, with:EntityType):Void {
switch [entity, with] {
case [EntityType.BULLET(_), EntityType.EAGLE(eagle)]:

View File

@@ -278,8 +278,8 @@ class BonusItem extends AnimateItem<Bonus> {
}
override public function redraw():Void {
var image = Assets.getBitmapData('resources/image/bonus/${value.bonusType}.png');
view.frames = AnimateBundle.bonusFrames(value.bonusType);
var image = Assets.getBitmapData('resources/image/bonus/${value.config.type}.png');
view.frames = AnimateBundle.bonusFrames(value.config.type);
view.playing = true;
}
}

View File

@@ -173,9 +173,9 @@ tanks:
skin: bd
bonuses:
- {type: clock}
- {type: clock, duration: 10}
- {type: grenade}
- {type: helmet}
- {type: helmet, duration: 20}
- {type: life}
- {type: shovel}
- {type: shovel, duration: 10}
- {type: star}

View File

@@ -136,9 +136,9 @@ tanks:
skin: bb
bonuses:
- {type: clock}
- {type: clock, duration: 10}
- {type: grenade}
- {type: helmet}
- {type: helmet, duration: 20}
- {type: life}
- {type: shovel}
- {type: shovel, duration: 10}
- {type: star}

View File

@@ -53,6 +53,7 @@ typedef TankConfig = {
typedef BonusConfig = {
var type:BonusType;
@:optional var duration:Int;
}
typedef TankSpawn = {

View File

@@ -1,15 +1,15 @@
package ru.m.tankz.core;
import ru.m.tankz.config.Config;
import ru.m.geom.Rectangle;
import ru.m.tankz.Type;
class Bonus extends Entity {
public var bonusType(default, null):BonusType;
public var config(default, null):BonusConfig;
public function new(bonusType:BonusType) {
public function new(config:BonusConfig) {
super(new Rectangle(0, 0, 44, 44));
this.bonusType = bonusType;
this.config = config;
}
}

View File

@@ -14,6 +14,7 @@ import ru.m.tankz.map.LevelMap;
interface EngineListener {
public function onSpawn(entity:EntityType):Void;
public function onChange(entity:EntityType):Void;
public function onCollision(entity:EntityType, with:EntityType):Void;
public function onDestroy(entity:EntityType):Void;
}
@@ -28,6 +29,8 @@ class CollisionProcessor implements EngineListener {
public function onSpawn(entity:EntityType):Void {}
public function onChange(entity:EntityType):Void {}
public function onCollision(entity:EntityType, with:EntityType):Void {
switch [entity, with] {
case [EntityType.TANK(tank), EntityType.TANK(other_tank)]:
@@ -44,6 +47,7 @@ class CollisionProcessor implements EngineListener {
if (!tank.protect.active) {
if (tank.hits > 0) {
tank.hits--;
engine.change(tank);
} else {
engine.destroy(tank);
}
@@ -59,6 +63,7 @@ class CollisionProcessor implements EngineListener {
engine.destroy(bullet);
if (!eagle.protect.active) {
eagle.death = true;
engine.change(eagle);
}
case _:
}
@@ -67,8 +72,7 @@ class CollisionProcessor implements EngineListener {
public function onDestroy(entity:EntityType):Void { }
}
@:build(yield.parser.Parser.run())
//@:yield //ToDo: not working
@:yield
class Engine implements ControlHandler {
public var config(default, default):Config;
@@ -97,6 +101,11 @@ class Engine implements ControlHandler {
for (l in listeners) l.onSpawn(type);
}
public function change(entity:Entity):Void {
var type = EntityTypeResolver.of(entity);
for (l in listeners) l.onChange(type);
}
public function destroy(entity:Entity):Void {
if (entities.exists(entity.id)) {
var type = EntityTypeResolver.of(entity);

View File

@@ -158,6 +158,17 @@ class Game implements EngineListener {
}
}
public function onChange(entity:EntityType):Void {
switch (entity) {
case EntityType.EAGLE(eagle):
if (eagle.death) {
lose(eagle.team);
deferred.resolve(state);
}
case _:
}
}
public function onCollision(entity:EntityType, with:EntityType):Void {
switch entity {
case EntityType.TANK(tank):
@@ -168,9 +179,6 @@ class Game implements EngineListener {
switch [entity, with] {
case [EntityType.TANK(tank), EntityType.BONUS(bonus)]:
applyBonus(tank, bonus);
case [EntityType.BULLET(_), EntityType.EAGLE(eagle)]:
lose(eagle.team);
deferred.resolve(state);
case _:
}
}
@@ -217,7 +225,7 @@ class Game implements EngineListener {
private function spawnBonus(?type:BonusType):Void {
var bonusConfig:BonusConfig = type != null ? config.getBonus(type) : config.bonuses[Math.floor(Math.random() * config.bonuses.length)];
var bonus = new Bonus(bonusConfig.type);
var bonus = new Bonus(bonusConfig);
bonus.rect.x = Math.random() * engine.map.width;
bonus.rect.y = Math.random() * engine.map.height;
engine.spawn(bonus);
@@ -228,8 +236,7 @@ class Game implements EngineListener {
}
private function applyBonus(tank:Tank, bonus:Bonus):Void {
L.e('XXX', 'applyBonus: ${bonus.bonusType}');
switch (bonus.bonusType) {
switch (bonus.config.type) {
case 'life':
getPlayer(tank.playerId).life++;
case 'star':
@@ -238,21 +245,26 @@ class Game implements EngineListener {
} else {
tank.hits++;
}
engine.change(tank);
case 'grenade':
for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
engine.destroy(t);
}
case 'helmet':
tank.protect.on(20);
tank.protect.on(bonus.config.duration);
engine.change(tank);
case 'clock':
for (t in engine.iterTanks(alienTank(tank.playerId.team))) {
t.freezing.on(10);
t.freezing.on(bonus.config.duration);
engine.change(t);
}
case 'shovel':
// ToDo: protect eagle/area
var team:Team = teams[tank.playerId.team];
if (team.eagleId > 0) {
cast(engine.entities[team.eagleId], Eagle).protect.on(10);
var eagle:Eagle = cast(engine.entities[team.eagleId], Eagle);
eagle.protect.on(bonus.config.duration);
engine.change(eagle);
}
case _:
engine.destroy(tank); // :-D