From 3061a4088ce6eb6f4c506d32074cf6d173f11dda Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 8 Jan 2018 15:19:34 +0300 Subject: [PATCH] [common] tanks and bullets config --- src/client/haxe/ru/m/tankz/render/Render.hx | 31 +++++++++--------- src/common/haxe/ru/m/tankz/config/Config.hx | 35 +++++++++++++++++++-- src/common/haxe/ru/m/tankz/core/Bullet.hx | 15 +++++++-- src/common/haxe/ru/m/tankz/core/Tank.hx | 21 ++++++++----- src/common/haxe/ru/m/tankz/engine/Engine.hx | 15 +++++---- 5 files changed, 82 insertions(+), 35 deletions(-) diff --git a/src/client/haxe/ru/m/tankz/render/Render.hx b/src/client/haxe/ru/m/tankz/render/Render.hx index 2e008b0..b0f3796 100755 --- a/src/client/haxe/ru/m/tankz/render/Render.hx +++ b/src/client/haxe/ru/m/tankz/render/Render.hx @@ -157,23 +157,24 @@ class Render extends SpriteView implements IRender { if (layersForUpdate[entryLayer]) { var g:Graphics = entryLayer.graphics; g.clear(); - for (ent in game.entities) if (Std.is(ent, IMobileEntity)) { - var e:IMobileEntity = cast ent; - if (Std.is(e, Tank)) { + for (ent in game.entities) { + var image:String = null; + if (Std.is(ent, Tank)) { + image = 'resources/images/tank/player/tank_p0_${cast(ent, Tank).index}-0.png'; + } else if (Std.is(ent, Bullet)) { var m = new Matrix(); - m.rotate(calcRotate(e.direction)); - m.translate(e.rect.x, e.rect.y); - g.beginBitmapFill(Assets.getBitmapData("resources/images/tank/player/tank_p0_0-0.png"), m); - g.drawRect(e.rect.x, e.rect.y, e.rect.width, e.rect.height); - g.endFill(); - } else if (Std.is(e, Bullet)) { - var m = new Matrix(); - m.rotate(calcRotate(e.direction)); - m.translate(e.rect.x, e.rect.y); - g.beginBitmapFill(Assets.getBitmapData("resources/images/bullet/bullet_0.png"), m); - g.drawRect(e.rect.x, e.rect.y, e.rect.width, e.rect.height); - g.endFill(); + image = 'resources/images/bullet/bullet_0.png'; + } else { + image = 'ERROR'; // ToDo: } + var m = new Matrix(); + if (Std.is(ent, IMobileEntity)) { + m.rotate(calcRotate(cast(ent, IMobileEntity).direction)); + } + m.translate(ent.rect.x, ent.rect.y); + g.beginBitmapFill(Assets.getBitmapData(image), m); + g.drawRect(ent.rect.x, ent.rect.y, ent.rect.width, ent.rect.height); + g.endFill(); } layersForUpdate[entryLayer] = false; } diff --git a/src/common/haxe/ru/m/tankz/config/Config.hx b/src/common/haxe/ru/m/tankz/config/Config.hx index 57fd290..6d479ee 100644 --- a/src/common/haxe/ru/m/tankz/config/Config.hx +++ b/src/common/haxe/ru/m/tankz/config/Config.hx @@ -1,7 +1,9 @@ package ru.m.tankz.config; +import ru.m.tankz.core.Bullet.BulletType; +import ru.m.tankz.core.Bullet; import openfl.utils.Assets; -import ru.m.tankz.map.Brick.BrickType; +import ru.m.tankz.map.Brick; import ru.m.tankz.proto.core.GameType; import ru.m.geom.Direction; @@ -14,6 +16,21 @@ typedef MapConfig = { var bricks:Array; } +typedef BulletConfig = { + var width:Float; + var height:Float; + var speed:Float; + var type:BulletType; +} + +typedef TankConfig = { + var width:Float; + var height:Float; + var speed:Float; + var bullet:BulletConfig; + var bullets:Int; +} + enum SpawnPointType { PLAYER; BOT; @@ -51,7 +68,6 @@ class Config { class ConfigBundle { - public static var CLASSIC:Config = new Config( { cellWidth: 22, @@ -78,6 +94,21 @@ class ConfigBundle { ] ); + public static var BULLET_A:BulletConfig = { + width: 12, + height: 12, + speed: 5, + type: BulletType.NORMAL + } + + public static var PLAYER_TANK_A:TankConfig = { + width: 36, + height: 36, + speed: 3, + bullet: BULLET_A, + bullets: 1 + } + public static function get(type:Int, level:Int):Config { switch (type) { case GameType.CLASSIC: diff --git a/src/common/haxe/ru/m/tankz/core/Bullet.hx b/src/common/haxe/ru/m/tankz/core/Bullet.hx index f993d40..fe2fbff 100644 --- a/src/common/haxe/ru/m/tankz/core/Bullet.hx +++ b/src/common/haxe/ru/m/tankz/core/Bullet.hx @@ -1,15 +1,24 @@ package ru.m.tankz.core; +import ru.m.tankz.config.Config.BulletConfig; import ru.m.geom.Rectangle; import ru.m.geom.Direction; -class Bullet extends MobileEntity { +@:enum abstract BulletType(Int) from Int to Int { + var NORMAL = 1; + var PIERCING = 2; +} + +class Bullet extends MobileEntity { public var tankId(default, null):Int; - public function new(tankId:Int, x:Float, y:Float, speed:Float, direction:Direction) { - super(new Rectangle(x, y, 12, 12), speed, direction); + private var config:BulletConfig; + + public function new(tankId:Int, config:BulletConfig) { + super(new Rectangle(0, 0, config.width, config.height), config.speed, Direction.TOP); this.tankId = tankId; + this.config = config; } } diff --git a/src/common/haxe/ru/m/tankz/core/Tank.hx b/src/common/haxe/ru/m/tankz/core/Tank.hx index c2f85fc..fcd279b 100755 --- a/src/common/haxe/ru/m/tankz/core/Tank.hx +++ b/src/common/haxe/ru/m/tankz/core/Tank.hx @@ -1,5 +1,7 @@ package ru.m.tankz.core; +import ru.m.tankz.config.Config.TankConfig; +import ru.m.tankz.core.Bullet; import ru.m.geom.Rectangle; import ru.m.geom.Direction; @@ -11,22 +13,27 @@ enum TankAction { } class Tank extends MobileEntity { + public var index(default, null):Int; - public var bulletsCount:Int = 0; + private var bulletsCounter:Int = 0; + private var config:TankConfig; - public function new(x:Float, y:Float, direction:Direction) { - super(new Rectangle(x, y, 36, 36), 4, direction); + public function new(index:Int, config: TankConfig) { + super(new Rectangle(0, 0, config.width, config.height), config.speed, Direction.TOP); + this.index = index; + this.config = config; } public function shot():Null { - if (bulletsCount >= 5) return null; - var bullet = new Bullet(id, rect.x + rect.width / 2 - 5, rect.y + rect.height / 2 - 5, 6, direction); + if (bulletsCounter >= config.bullets) return null; + var bullet = new Bullet(id, config.bullet); + bullet.rect.center = rect.center; bullet.move(direction); - bulletsCount++; + bulletsCounter++; return bullet; } public function onDestroyBullet():Void { - bulletsCount--; + bulletsCounter--; } } diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index 10b72f8..42e9888 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -1,5 +1,6 @@ package ru.m.tankz.engine; +import ru.m.tankz.config.Config.TankConfig; import ru.m.tankz.core.IEntity; import ru.m.geom.Point; import ru.m.tankz.map.Brick; @@ -32,9 +33,6 @@ class Engine implements IEngine { public var entities(default, null):Map; public var removedEntities(default, null):Array; - private var x_limit:Float; - private var y_limit:Float; - private var playerTanks(default, null):Map; private var time:Float; @@ -44,8 +42,11 @@ class Engine implements IEngine { playerTanks = new Map(); } - private function buildTank(cellX:Float, cellY:Float, direction:Direction):Tank { - return new Tank(cellX * map.cellWidth, cellY * map.cellHeight, direction); + private function buildTank(index:Int, config:TankConfig, point:SpawnPoint):Tank { + var tank = new Tank(index, config); + tank.rect.center = new Point((point.x + 1) * map.cellWidth, (point.y + 1) * map.cellHeight); + tank.direction = point.direction; + return tank; } public function init(config:Config):Void { @@ -54,8 +55,6 @@ class Engine implements IEngine { playerTanks = new Map(); entities = new Map(); removedEntities = new Array(); - x_limit = map.gridWidth * map.cellWidth; - y_limit = map.gridHeight * map.cellHeight; time = Date.now().getTime(); } @@ -64,7 +63,7 @@ class Engine implements IEngine { for (index in 0...players.length) { var player:Player = players[index]; var point:SpawnPoint = config.getSpawnPoint(SpawnPointType.PLAYER, index); - var tank = buildTank(point.x, point.y, point.direction); + var tank = buildTank(index, ConfigBundle.PLAYER_TANK_A, point); playerTanks.set(player.id, tank); entities.set(tank.id, tank); changes.push(new GameChange()