[common] Render implement EngineListener
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.render;
|
package ru.m.tankz.render;
|
||||||
|
|
||||||
|
import ru.m.tankz.core.EntityType;
|
||||||
import flash.display.DisplayObject;
|
import flash.display.DisplayObject;
|
||||||
import Type.ValueType;
|
import Type.ValueType;
|
||||||
import ru.m.tankz.render.RenderItem;
|
import ru.m.tankz.render.RenderItem;
|
||||||
@@ -11,7 +12,7 @@ import flash.display.Graphics;
|
|||||||
import haxework.gui.SpriteView;
|
import haxework.gui.SpriteView;
|
||||||
|
|
||||||
|
|
||||||
class Render extends SpriteView {
|
class Render extends SpriteView implements EngineListener {
|
||||||
|
|
||||||
private var backgroundLayer:Sprite;
|
private var backgroundLayer:Sprite;
|
||||||
private var groundLayer:Sprite;
|
private var groundLayer:Sprite;
|
||||||
@@ -59,23 +60,6 @@ class Render extends SpriteView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (entity in game.entities) {
|
|
||||||
if (!items.exists(entity.key)) {
|
|
||||||
items[entity.key] = switch (Type.typeof(entity)) {
|
|
||||||
case ValueType.TClass(Tank): cast new TankItem(cast entity);
|
|
||||||
case ValueType.TClass(Bullet): cast new BulletItem(cast entity);
|
|
||||||
case x: null;
|
|
||||||
}
|
|
||||||
entryLayer.addChild(items[entity.key].view);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*for (key in game.removedEntities) {
|
|
||||||
if (items.exists(key)) {
|
|
||||||
var view:DisplayObject = items[key].view;
|
|
||||||
view.parent.removeChild(view);
|
|
||||||
items.remove(key);
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
for (item in items) {
|
for (item in items) {
|
||||||
item.update();
|
item.update();
|
||||||
}
|
}
|
||||||
@@ -91,4 +75,41 @@ class Render extends SpriteView {
|
|||||||
background = null;
|
background = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function onSpawn(entity:EntityType):Void {
|
||||||
|
switch(entity) {
|
||||||
|
case EntityType.TANK(tank):
|
||||||
|
var item = new TankItem(tank);
|
||||||
|
items.set(tank.key, item);
|
||||||
|
entryLayer.addChild(item.view);
|
||||||
|
item.update();
|
||||||
|
case EntityType.BULLET(bullet):
|
||||||
|
var item = new BulletItem(bullet);
|
||||||
|
items.set(bullet.key, item);
|
||||||
|
entryLayer.addChild(item.view);
|
||||||
|
item.update();
|
||||||
|
case _:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onCollision(entity:EntityType, with:EntityType):Void {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onDestroy(entity:EntityType):Void {
|
||||||
|
switch(entity) {
|
||||||
|
case EntityType.TANK(tank):
|
||||||
|
if (items.exists(tank.key)) {
|
||||||
|
entryLayer.removeChild(items.get(tank.key).view);
|
||||||
|
items.remove(tank.key);
|
||||||
|
}
|
||||||
|
case EntityType.BULLET(bullet):
|
||||||
|
if (items.exists(bullet.key)) {
|
||||||
|
entryLayer.removeChild(items.get(bullet.key).view);
|
||||||
|
items.remove(bullet.key);
|
||||||
|
}
|
||||||
|
case _:
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package ru.m.tankz.view.frames;
|
package ru.m.tankz.view.frames;
|
||||||
|
|
||||||
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.control.PlayerControl;
|
import ru.m.tankz.control.PlayerControl;
|
||||||
import flash.events.Event;
|
import flash.events.Event;
|
||||||
import haxe.Timer;
|
import haxe.Timer;
|
||||||
@@ -28,10 +29,16 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function onShow():Void {
|
public function onShow():Void {
|
||||||
game = new ClassicGame(ConfigBundle.get(ClassicGame.TYPE, 0));
|
var config:Config = Provider.get(Config);
|
||||||
|
game = switch (config.type) {
|
||||||
|
case ClassicGame.TYPE: new ClassicGame(config);
|
||||||
|
case x: throw 'Unsupported game type "${x}"';
|
||||||
|
}
|
||||||
|
game.engine.listeners.push(render);
|
||||||
game.start();
|
game.start();
|
||||||
game.setControl({team:'human', index:0}, PlayerControl.forPlayer(0));
|
for (index in 0...game.config.getTeam('human').size) {
|
||||||
game.setControl({team:'human', index:1}, PlayerControl.forPlayer(1));
|
game.setControl({team:'human', index:index}, PlayerControl.forPlayer(index));
|
||||||
|
}
|
||||||
content.addEventListener(Event.ENTER_FRAME, redraw);
|
content.addEventListener(Event.ENTER_FRAME, redraw);
|
||||||
Provider.get(IConnection).packetHandler.addListener(this);
|
Provider.get(IConnection).packetHandler.addListener(this);
|
||||||
render.draw(game.engine);
|
render.draw(game.engine);
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package ru.m.tankz.view.frames;
|
package ru.m.tankz.view.frames;
|
||||||
|
|
||||||
import ru.m.tankz.proto.core.GameType;
|
import ru.m.tankz.config.Config;
|
||||||
import ru.m.tankz.proto.core.Player;
|
import ru.m.tankz.game.ClassicGame;
|
||||||
import ru.m.tankz.proto.core.Game;
|
import ru.m.tankz.config.ConfigBundle;
|
||||||
import haxework.gui.frame.IFrameSwitcher;
|
import haxework.gui.frame.IFrameSwitcher;
|
||||||
import haxework.provider.Provider;
|
import haxework.provider.Provider;
|
||||||
import haxework.gui.ButtonView;
|
import haxework.gui.ButtonView;
|
||||||
@@ -21,23 +21,18 @@ class StartFrame extends VGroupView implements ViewBuilder {
|
|||||||
|
|
||||||
public function onPress(view:ButtonView):Void {
|
public function onPress(view:ButtonView):Void {
|
||||||
switch (view.id) {
|
switch (view.id) {
|
||||||
case "start_1p":
|
case 'start_1p':
|
||||||
startGame(1);
|
startGame(1);
|
||||||
case "start_2p":
|
case 'start_2p':
|
||||||
startGame(2);
|
startGame(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function startGame(playersCount:Int):Void {
|
private function startGame(players:Int):Void {
|
||||||
var game = new Game();
|
var config = ConfigBundle.get(ClassicGame.TYPE, 0);
|
||||||
game.type = GameType.CLASSIC;
|
config.getTeam('human').size = players;
|
||||||
for (i in 0...playersCount) {
|
config.getTeam('bot').size = 2 + 2 * players;
|
||||||
var player = new Player();
|
Provider.set(Config, config);
|
||||||
player.id = i;
|
|
||||||
game.players.push(player);
|
|
||||||
}
|
|
||||||
game.id = 1;
|
|
||||||
Provider.set(Game, game);
|
|
||||||
Provider.get(IFrameSwitcher).change(GameFrame.ID);
|
Provider.get(IFrameSwitcher).change(GameFrame.ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ teams:
|
|||||||
direction: top
|
direction: top
|
||||||
- id: bot
|
- id: bot
|
||||||
size: 6
|
size: 6
|
||||||
spawnInterval: 1000
|
spawnInterval: 3000
|
||||||
points:
|
points:
|
||||||
- type: tank
|
- type: tank
|
||||||
index: -1
|
index: -1
|
||||||
@@ -51,6 +51,7 @@ typedef TeamConfig = {
|
|||||||
|
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
|
public var type(default, null):String;
|
||||||
public var map(default, null):MapConfig;
|
public var map(default, null):MapConfig;
|
||||||
public var bricks(default, null):Array<BrickConfig>;
|
public var bricks(default, null):Array<BrickConfig>;
|
||||||
public var tanks(default, null):Array<TankConfig>;
|
public var tanks(default, null):Array<TankConfig>;
|
||||||
@@ -60,7 +61,8 @@ class Config {
|
|||||||
private var tankMap:Map<String, Map<String, TankConfig>>;
|
private var tankMap:Map<String, Map<String, TankConfig>>;
|
||||||
private var teamMap:Map<String, TeamConfig>;
|
private var teamMap:Map<String, TeamConfig>;
|
||||||
|
|
||||||
public function new(map:MapConfig, bricks:Array<BrickConfig>, teams:Array<TeamConfig>, tanks:Array<TankConfig>) {
|
public function new(type:String, map:MapConfig, bricks:Array<BrickConfig>, teams:Array<TeamConfig>, tanks:Array<TankConfig>) {
|
||||||
|
this.type = type;
|
||||||
this.map = map;
|
this.map = map;
|
||||||
this.bricks = bricks;
|
this.bricks = bricks;
|
||||||
this.teams = teams;
|
this.teams = teams;
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class ConfigBundle {
|
|||||||
public static function get(type:String, level:Int):Config {
|
public static function get(type:String, level:Int):Config {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ClassicGame.TYPE:
|
case ClassicGame.TYPE:
|
||||||
var source:ConfigSource = convert(Yaml.parse(Assets.getText('resources/config/config.yaml'), Parser.options().useObjects()));
|
var source:ConfigSource = convert(Yaml.parse(Assets.getText('resources/config/${type}.yaml'), Parser.options().useObjects()));
|
||||||
var bricksData:String = Assets.getText('resources/levels/level00${level}.txt');
|
var bricksData:String = Assets.getText('resources/levels/level00${level}.txt');
|
||||||
var bricks:Array<BrickConfig> = [];
|
var bricks:Array<BrickConfig> = [];
|
||||||
for (line in ~/\s+/g.split(bricksData)) {
|
for (line in ~/\s+/g.split(bricksData)) {
|
||||||
@@ -42,7 +42,7 @@ class ConfigBundle {
|
|||||||
tanks.push(item);
|
tanks.push(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new Config(source.map, source.bricks, source.teams, tanks);
|
return new Config(type, source.map, source.bricks, source.teams, tanks);
|
||||||
case _:
|
case _:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,6 +104,12 @@ class Engine {
|
|||||||
public function destroy(entity:Entity):Void {
|
public function destroy(entity:Entity):Void {
|
||||||
if (entities.exists(entity.id)) {
|
if (entities.exists(entity.id)) {
|
||||||
var type = EntityTypeResolver.of(entity);
|
var type = EntityTypeResolver.of(entity);
|
||||||
|
switch (type) {
|
||||||
|
case EntityType.BULLET(bullet):
|
||||||
|
var tank:Tank = cast entities.get(bullet.tankId);
|
||||||
|
if (tank != null) tank.onDestroyBullet();
|
||||||
|
case _:
|
||||||
|
}
|
||||||
for (l in listeners) l.onDestroy(type);
|
for (l in listeners) l.onDestroy(type);
|
||||||
entities.remove(entity.id);
|
entities.remove(entity.id);
|
||||||
}
|
}
|
||||||
@@ -122,7 +128,7 @@ class Engine {
|
|||||||
case TankAction.SHOT:
|
case TankAction.SHOT:
|
||||||
var bullet = tank.shot();
|
var bullet = tank.shot();
|
||||||
if (bullet != null) {
|
if (bullet != null) {
|
||||||
entities.set(bullet.id, bullet);
|
spawn(bullet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -192,4 +198,8 @@ class Engine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function dispose():Void {
|
||||||
|
listeners = [];
|
||||||
|
entities = new Map();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import ru.m.tankz.config.Config;
|
|||||||
|
|
||||||
class ClassicGame extends Game {
|
class ClassicGame extends Game {
|
||||||
|
|
||||||
public static var TYPE(default, never):GameType = Type.getClassName(ClassicGame);
|
public static var TYPE(default, never):GameType = 'classic';
|
||||||
|
|
||||||
public function new(config:Config) {
|
public function new(config:Config) {
|
||||||
super(TYPE, config);
|
super(TYPE, config);
|
||||||
|
|||||||
@@ -138,8 +138,8 @@ class Game implements EngineListener implements ControlListener {
|
|||||||
private function spawn(task:SpawnTask):Void {
|
private function spawn(task:SpawnTask):Void {
|
||||||
var tank = buildTank(task.playerId, config.getTank(task.playerId.team, '0'), task.point);
|
var tank = buildTank(task.playerId, config.getTank(task.playerId.team, '0'), task.point);
|
||||||
var player:Player = teams.get(task.playerId.team).players[task.playerId.index];
|
var player:Player = teams.get(task.playerId.team).players[task.playerId.index];
|
||||||
player.tankId = tank.id;
|
|
||||||
engine.spawn(tank);
|
engine.spawn(tank);
|
||||||
|
player.tankId = tank.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setControl(playerId:PlayerId, control:Control):Void {
|
public function setControl(playerId:PlayerId, control:Control):Void {
|
||||||
@@ -185,6 +185,6 @@ class Game implements EngineListener implements ControlListener {
|
|||||||
|
|
||||||
|
|
||||||
public function dispose():Void {
|
public function dispose():Void {
|
||||||
|
engine.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user