[common] added Bonus entity

This commit is contained in:
2018-02-09 16:57:33 +03:00
parent 5c57454998
commit 41c5610f9a
22 changed files with 182 additions and 40 deletions

View File

@@ -10,8 +10,12 @@ typedef TeamId = String;
typedef ControlType = String;
typedef BrickType = Int;
typedef TankType = String;
typedef BonusType = String;
typedef PlayerId = {
var team:TeamId;
var type:ControlType;

View File

@@ -26,7 +26,7 @@ typedef MapConfig = {
}
typedef BrickConfig = {
var type:Int;
var type:BrickType;
var layer:Int;
var armor:Int;
}
@@ -47,11 +47,17 @@ typedef TankConfig = {
var bullets:Int;
var hits:Int;
var skin:String;
@:optinal var upgrade:TankType;
}
typedef BonusConfig = {
var type:BonusType;
}
typedef TankSpawn = {
var type:TankType;
var rate: Float;
var rate:Float;
@:optional var bonus:Float;
}
@@ -78,10 +84,12 @@ class Config {
public var tanks(default, null):Array<TankConfig>;
public var teams(default, null):Array<TeamConfig>;
public var points(default, null):Array<SpawnPoint>;
public var bonuses(default, null):Array<BonusConfig>;
private var brickMap:Map<Int, BrickConfig>;
private var tankMap:Map<TankType, TankConfig>;
private var teamMap:Map<String, TeamConfig>;
private var teamMap:Map<TeamId, TeamConfig>;
private var bonusMap:Map<BonusType, BonusConfig>;
public function new(
type:String,
@@ -90,7 +98,8 @@ class Config {
bricks:Array<BrickConfig>,
teams:Array<TeamConfig>,
points:Array<SpawnPoint>,
tanks:Array<TankConfig>
tanks:Array<TankConfig>,
bonuses:Array<BonusConfig>
) {
this.type = type;
this.game = game;
@@ -99,6 +108,7 @@ class Config {
this.teams = teams;
this.points = points;
this.tanks = tanks;
this.bonuses = bonuses;
init();
}
@@ -115,6 +125,10 @@ class Config {
for (item in tanks) {
tankMap.set(item.type, item);
}
bonusMap = new Map();
for (item in bonuses) {
bonusMap.set(item.type, item);
}
}
public function getBrick(type:Int):BrickConfig {
@@ -128,4 +142,8 @@ class Config {
public function getTank(type:TankType):TankConfig {
return tankMap.get(type);
}
public function getBonus(type:BonusType):BonusConfig {
return bonusMap.get(type);
}
}

View File

@@ -13,6 +13,7 @@ typedef ConfigSource = {
var teams: Array<TeamConfig>;
var points: Array<SpawnPoint>;
var tanks: Array<TankConfig>;
var bonuses: Array<BonusConfig>;
}
class ConfigBundle {
@@ -23,6 +24,6 @@ class ConfigBundle {
public static function get(type:String):Config {
var source = convert(Yaml.parse(Assets.getText('resources/${type}/config.yaml'), Parser.options().useObjects()));
return new Config(type, source.game, source.map, source.bricks, source.teams, source.points, source.tanks);
return new Config(type, source.game, source.map, source.bricks, source.teams, source.points, source.tanks, source.bonuses);
}
}

View File

@@ -8,7 +8,7 @@ import ru.m.tankz.Type;
enum TankAction {
MOVE(direction:Direction);
LEVEL_UP(level:Int);
UPGRADE;
STOP;
SHOT;
}

View File

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

View File

@@ -1,7 +1,7 @@
package ru.m.tankz.core;
import Type;
import ru.m.tankz.map.Grid.GridCell;
import ru.m.tankz.map.Grid;
enum EntityType {
@@ -9,6 +9,7 @@ enum EntityType {
TANK(tank:Tank);
BULLET(bullet:Bullet);
CELL(cell:GridCell);
BONUS(bonus:Bonus);
}
@@ -20,6 +21,7 @@ class EntityTypeResolver {
case ValueType.TClass(Tank): EntityType.TANK(cast entity);
case ValueType.TClass(Bullet): EntityType.BULLET(cast entity);
case ValueType.TClass(GridCell): EntityType.CELL(cast entity);
case ValueType.TClass(Bonus): EntityType.BONUS(cast entity);
case x: null;
}
}

View File

@@ -14,6 +14,7 @@ class Tank extends MobileEntity {
public var config(default, set):TankConfig;
public var color(default, default):Color;
public var hits(default, default):Int;
public var bonus(default, default):Bool;
private var bulletsCounter:Int = 0;

View File

@@ -58,6 +58,8 @@ class CollisionProcessor implements EngineListener {
tank1.rect.lean(eagle.rect);
case EntityType.CELL(cell):
tank1.rect.lean(cell.rect);
case EntityType.BONUS(bonus):
engine.destroy(bonus);
}
case EntityType.BULLET(bullet1):
switch (with) {
@@ -74,6 +76,7 @@ class CollisionProcessor implements EngineListener {
engine.destroy(eagle);
case EntityType.CELL(cell):
engine.destroy(bullet1);
case EntityType.BONUS(bonus):
}
case _:
}
@@ -131,15 +134,10 @@ class Engine implements ControlHandler {
switch (action) {
case TankAction.MOVE(direction):
tank.move(direction);
case TankAction.LEVEL_UP(level):
// ToDo:
var newType = switch(tank.config.type) {
case 'human0': 'human1';
case 'human1': 'human2';
case 'human2': 'human3';
case x: 'human3';
case TankAction.UPGRADE:
if (tank.config.upgrade != null) {
tank.config = config.getTank(tank.config.upgrade);
}
tank.config = config.getTank(newType);
case TankAction.STOP:
tank.stop();
case TankAction.SHOT:

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game;
import ru.m.tankz.core.Bonus;
import haxe.ds.Option;
import haxe.Timer;
import promhx.Deferred;
@@ -54,6 +55,7 @@ class Game implements EngineListener {
var tankConfig:TankConfig = config.getTank(spawn.type);
var tank = new Tank(playerId, tankConfig);
tank.color = playerId.color.zero ? teams[playerId.team].config.color : playerId.color;
tank.bonus = Math.random() < spawn.bonus;
applyPoint(tank, point);
return tank;
}
@@ -154,6 +156,11 @@ class Game implements EngineListener {
case EntityType.TANK(tank):
var control = getPlayer(tank.playerId).control;
if (control != null) control.onCollision(with);
switch (with) {
case EntityType.BONUS(bonus):
applyBonus(tank, bonus);
case x:
}
case x:
}
}
@@ -207,6 +214,7 @@ class Game implements EngineListener {
state.teams[tank.playerId.team].lose = true;
complete();
}
if (tank.bonus) spawnBonus();
deferred.resolve(state);
case EntityType.EAGLE(eagle):
state.teams[eagle.team].lose = true;
@@ -216,18 +224,53 @@ class Game implements EngineListener {
}
}
public function onAction(tankId:Int, action:TankAction):Void {
engine.action(tankId, action);
}
public function next():Option<GameState> {
return Option.None;
}
public function dispose():Void {
engine.dispose();
}
private function spawnBonus():Void {
var bonusConfig = config.bonuses[Math.floor(Math.random() * config.bonuses.length)];
L.d(TAG, 'Spawn Bonus(${bonusConfig}');
var bonus = new Bonus(bonusConfig.type);
bonus.rect.x = Math.random() * engine.map.width;
bonus.rect.y = Math.random() * engine.map.height;
engine.spawn(bonus);
}
private function applyBonus(tank:Tank, bonus:Bonus):Void {
switch (bonus.bonusType) {
case 'life':
state.teams[tank.playerId.team].players[tank.playerId.index].life++;
case 'star':
if (tank.config.upgrade != null) {
tank.config = config.getTank(tank.config.upgrade);
} else {
tank.hits++;
}
case 'grenade':
for (entity in engine.entities.iterator()) {
switch (EntityTypeResolver.of(entity)) {
case EntityType.TANK(t):
if (t.playerId.team != tank.playerId.team) {
engine.destroy(t);
}
case x:
}
}
case 'helmet':
case 'clock':
case 'shovel':
case x:
engine.destroy(tank); // :-D
}
}
}

View File

@@ -15,6 +15,9 @@ class LevelMap {
public var gridWidth(default, null):Int;
public var gridHeight(default, null):Int;
public var width(get, null):Float;
public var height(get, null):Float;
public var bricks(default, null):Array<Brick>;
public var grid(default, null):Grid;
@@ -68,4 +71,12 @@ class LevelMap {
var cellY:Int = Math.floor(point.y / config.cellHeight);
return bricks[cellX + cellY * config.gridWidth];
}
private inline function get_width():Float {
return config.cellWidth * config.gridWidth;
}
private inline function get_height():Float {
return config.cellHeight * config.gridHeight;
}
}