[all] fixes for LevelPack

This commit is contained in:
2019-06-17 11:45:33 +03:00
parent e39de4467d
commit c0ab953115
24 changed files with 94 additions and 85 deletions

View File

@@ -1,10 +1,5 @@
package ru.m.tankz.bundle;
import haxe.io.BytesInput;
import haxe.zip.Entry;
import haxe.zip.Reader;
import lime.utils.Bytes;
import lime.utils.CompressionAlgorithm;
import openfl.Assets;
import ru.m.tankz.config.Config;
import ru.m.tankz.Type;
@@ -14,25 +9,12 @@ class LevelBundle implements ILevelBundle {
public function new() {}
private function extract(entry:Entry):LevelConfig {
var bytes:Bytes = entry.data;
if (entry.compressed) {
bytes = bytes.decompress(CompressionAlgorithm.DEFLATE);
}
var level = LevelUtil.loads(bytes.toString());
if (level.id == null) {
level.id = Std.parseInt(entry.fileName.split("level").pop());
}
return level;
}
public function get(type:GameType, name:String):LevelPack {
var data = Assets.getBytes('levels/${type}_${name}.zip');
var files = Reader.readZip(new BytesInput(data));
var bytes = Assets.getBytes('levels/${type}_${name}.zip');
return {
type: type,
name: name,
data: Lambda.array(files.map(extract)),
data: LevelUtil.unpack(bytes),
};
}
}

View File

@@ -1,18 +1,19 @@
package ru.m.tankz.local;
import ru.m.tankz.local.LocalControlFactory;
import ru.m.tankz.config.Config;
import ru.m.tankz.game.GameEvent;
import ru.m.tankz.game.GameRunner;
import ru.m.tankz.game.GameState;
import ru.m.tankz.game.record.GameRecorder;
import ru.m.tankz.local.LocalControlFactory;
import ru.m.tankz.storage.RecordStorage;
class LocalGame extends GameRunner {
@:provide var recordStorage:RecordStorage;
private var recorder:GameRecorder;
public function new(state:GameState) {
super(state);
public function new(state:GameState, level:LevelConfig) {
super(state, level);
controlFactory = new LocalControlFactory();
recorder = new GameRecorder();
connect(recorder);

View File

@@ -14,7 +14,7 @@ class NetworkGame extends Game {
private var network:NetworkManager;
public function new(network:NetworkManager) {
super(new GameState(network.room.game.type, 0));
super(new GameState(network.room.game.type, 0), null);
this.network = network;
this.controlFactory = new NetworkControlFactory();
network.gameEventSignal.connect(onGameEventProto);

View File

@@ -115,7 +115,8 @@ class Render extends SpriteView implements IRender {
public function onGameEvent(event:GameEvent):Void {
switch event {
case START(_):
case START(_, level):
gridSize = level.size;
content.addEventListener(Event.ENTER_FRAME, onEnterFrame);
case COMPLETE(_, _):
content.removeEventListener(Event.ENTER_FRAME, onEnterFrame);

View File

@@ -31,7 +31,6 @@ import ru.m.tankz.view.game.GameView;
gameView.type = game.type;
soundManager.config = game.config;
gameView.render.config = game.config;
gameView.render.gridSize = game.state.level.size;
game.connect(gameView.render);
game.connect(soundManager);
if (gameView.panel != null) {

View File

@@ -34,10 +34,9 @@ import ru.m.tankz.view.popup.LevelPopup;
}
private function start(level:LevelConfig, preset:GamePreset, control:ControlPreset):Void {
state.level = level;
state.presetId = preset.id;
state.controls = control.values;
game = new LocalGame(state);
game = new LocalGame(state, level);
switcher.change(GameFrame.ID);
}

View File

@@ -1,6 +1,5 @@
package ru.m.tankz.view;
import haxework.view.ButtonView;
import haxework.view.DataView;
import haxework.view.frame.FrameSwitcher;
import haxework.view.LabelView;
@@ -18,7 +17,6 @@ import ru.m.tankz.view.common.LifeView;
@:provide var frames:FrameSwitcher;
@:provide var state:GameState;
@:provide("next") var nextState:GameState;
@:provide var game:IGame;
private function playerViewFactory(index:Int, player:PlayerState) {
@@ -35,14 +33,11 @@ import ru.m.tankz.view.common.LifeView;
public function onShow() {
resultView.data = Lambda.array(state.players);
levelLabel.text = 'Level ${state.levelId}';
//levelLabel.text = 'Level ${state.levelId}'; // ToDo: level?
}
private function next() {
if (nextState != null) {
game = new GameRunner(nextState);
frames.change(GameFrame.ID);
}
//ToDo: next level?
}
private function close() {

View File

@@ -21,7 +21,6 @@ import ru.m.tankz.view.common.LifeView;
private var player2Id:PlayerId = new PlayerId(ClassicGame.HUMAN, 1);
public function refresh(state:GameState):Void {
level.text = 'Level ${state.levelId}';
bot.life = state.getTeamLife(ClassicGame.BOT);
player1.life = state.getPlayerLife(player1Id);
player1.score = state.getPlayerScore(player1Id);
@@ -36,7 +35,8 @@ import ru.m.tankz.view.common.LifeView;
public function onGameEvent(event:GameEvent):Void {
switch event {
case START(state):
case START(state, level):
this.level.text = 'Level ${level.id}';
refresh(state);
case CHANGE(TEAM_LIFE(teamId, life)):
if (teamId == ClassicGame.BOT) {

View File

@@ -16,8 +16,8 @@ import ru.m.tankz.view.common.LifeView;
public function onGameEvent(event:GameEvent):Void {
switch event {
case START(state):
level.text = 'Level ${state.levelId}';
case START(state, level):
this.level.text = 'Level ${level.id}';
players.data = Lambda.array(state.players);
case _:
for (view in players.views) {

View File

@@ -17,7 +17,6 @@ import ru.m.tankz.view.common.LifeView;
@:view var level:LabelView;
public function refresh(state:GameState):Void {
level.text = 'Level ${state.levelId}';
radiant.life = state.getTeamLife(DotaGame.RADIANT);
radiant.score = state.getTeamScore(DotaGame.RADIANT);
dire.life = state.getTeamLife(DotaGame.DIRE);
@@ -34,7 +33,8 @@ import ru.m.tankz.view.common.LifeView;
public function onGameEvent(event:GameEvent):Void {
switch event {
case START(state):
case START(state, level):
this.level.text = 'Level ${level.id}';
refresh(state);
case CHANGE(TEAM_LIFE(teamId, life)):
getLifeView(teamId).life = life;

View File

@@ -30,7 +30,7 @@ import ru.m.tankz.view.popup.CreateGamePopup;
private function create():Void {
CreateGamePopup.instance.show().then(function(result) {
if (result != null) {
network.createGame(result.type, result.level);
network.createGame(result.type, result.level.id);
}
});
}

View File

@@ -4,22 +4,25 @@ import haxework.view.DataView;
import haxework.view.popup.PopupView;
import haxework.view.ToggleButtonView;
import ru.m.tankz.bundle.IConfigBundle;
import ru.m.tankz.bundle.ILevelBundle;
import ru.m.tankz.config.Config;
import ru.m.tankz.Type;
private typedef Result = {
var type:GameType;
var level:LevelId;
var level:LevelConfig;
}
@:template class CreateGamePopup extends PopupView<Result> {
@:view("type") var typeView:DataView<GameType, ToggleButtonView>;
@:view("level") var levelView:DataView<LevelId, ToggleButtonView>;
@:view("level") var levelView:DataView<LevelConfig, ToggleButtonView>;
@:provide var configBundle:IConfigBundle;
@:provide static var configBundle:IConfigBundle;
@:provide static var levelBundle:ILevelBundle;
private var type:GameType;
private var level:LevelId;
private var level:LevelConfig;
override private function onShow():Void {
super.onShow();
@@ -43,18 +46,18 @@ private typedef Result = {
for (v in typeView.dataViews) {
v.on = v == view;
}
levelView.data = [for (i in 0...configBundle.get(type).game.levels) i];
onLevelSelect(0, 0, levelView.dataViews[0]);
levelView.data = levelBundle.get(value, "standard").data;
onLevelSelect(0, levelView.data[0], levelView.dataViews[0]);
}
private function levelViewFactory(index:Int, value:LevelId):ToggleButtonView {
private function levelViewFactory(index:Int, value:LevelConfig):ToggleButtonView {
var result = new ToggleButtonView();
result.skinId = "button.level";
result.text = Std.string(value);
result.text = Std.string(value.id);
return result;
}
private function onLevelSelect(index:Int, level:LevelId, view:ToggleButtonView):Void {
private function onLevelSelect(index:Int, level:LevelConfig, view:ToggleButtonView):Void {
this.level = level;
for (v in levelView.dataViews) {
v.on = v == view;

View File

@@ -25,6 +25,7 @@ import ru.m.tankz.Type;
public var config(default, null):Config;
public var winner(default, null):Null<TeamId>;
public var state(default, null):GameState;
public var level(default, null):LevelConfig;
public var engine(default, null):IEngine;
public var controlFactory(default, null):IControlFactory;
public var pause(default, set):Bool;
@@ -33,9 +34,10 @@ import ru.m.tankz.Type;
@:provide var configBundle:IConfigBundle;
public function new(state:GameState) {
public function new(state:GameState, level:LevelConfig) {
this.type = state.type;
this.state = state;
this.level = level;
this.teams = new Map();
this.config = configBundle.get(type);
this.controlFactory = new NoneControlFactory();
@@ -71,8 +73,9 @@ import ru.m.tankz.Type;
public function onGameEvent(event:GameEvent):Void {
switch event {
case START(state):
case START(state, level):
this.state = state;
this.level = level;
case COMPLETE(state, winnerId):
this.state = state;
this.winner = winnerId;
@@ -98,8 +101,8 @@ import ru.m.tankz.Type;
}
public function start():Void {
var level:LevelConfig = state.level;
var points:Array<SpawnPoint> = level.points != null ? level.points : config.points;
// ToDo: Spawner not in Team?
var points:Array<SpawnPoint> = level != null && level.points != null ? level.points : config.points;
for (teamConfig in state.preset.teams) {
var teamPoints = points.filter(function(p:SpawnPoint) return p.team == teamConfig.id);
var team:Team = new Team(teamConfig, teamPoints, state.teams[teamConfig.id]);

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game;
import ru.m.tankz.config.Config.LevelConfig;
import haxework.color.Color;
import ru.m.geom.Position;
import ru.m.geom.Rectangle;
@@ -73,7 +74,7 @@ enum ChangeEvent {
}
enum GameEvent {
START(state:GameState);
START(state:GameState, level:LevelConfig);
SPAWN(event:SpawnEvent);
MOVE(event:MoveEvent);
STOP(event:StopEvent);

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.game;
import ru.m.tankz.config.Config.LevelConfig;
import ru.m.geom.Line;
import ru.m.geom.Point;
import ru.m.tankz.control.Control;
@@ -19,10 +20,10 @@ class GameRunner extends Game implements EngineListener {
private var timer:Timer;
private var builder:EntityBuilder;
public function new(state:GameState) {
super(state);
public function new(state:GameState, level:LevelConfig) {
super(state, level);
this.builder = new EntityBuilder(config);
this.engine = new Engine(config, state.level.size);
this.engine = new Engine(config, level.size);
this.engine.connect(this);
}
@@ -51,7 +52,7 @@ class GameRunner extends Game implements EngineListener {
override public function start():Void {
super.start();
var mapData = state.level.data.map(function(index:BrickIndex) return config.getBrickByIndex(index));
var mapData = level.data.map(function(index:BrickIndex) return config.getBrickByIndex(index));
engine.map.setData(mapData);
for (team in teams.iterator()) {
team.spawner.runner = spawn;
@@ -68,7 +69,7 @@ class GameRunner extends Game implements EngineListener {
}
}
gameEventSignal.emit(EventUtil.buildBricksSpawn(engine.map));
gameEventSignal.emit(START(state));
gameEventSignal.emit(START(state, level));
//for (i in 0...10) spawnBonus();
}

View File

@@ -2,7 +2,6 @@ package ru.m.tankz.game;
import haxework.color.Color;
import ru.m.tankz.bundle.IConfigBundle;
import ru.m.tankz.bundle.ILevelBundle;
import ru.m.tankz.config.Config;
import ru.m.tankz.Type;
@@ -72,20 +71,17 @@ class GameState {
public var type:GameType;
public var presetId:PresetId;
public var levelId(get, null):LevelId;
public var controls:Array<PlayerControl>;
public var players:Map<String, PlayerState>;
public var teams:Map<TeamId, TeamState>;
public var preset(get, null):GamePreset;
public var config(get, null):Config;
public var level(default, default):LevelConfig;
@:provide static private var configBundle:IConfigBundle;
public function new(type:GameType, presetId:PresetId = 0, level:LevelConfig = null, state:GameState = null, controls:Array<PlayerControl> = null) {
public function new(type:GameType, presetId:PresetId = 0, state:GameState = null, controls:Array<PlayerControl> = null) {
this.type = type;
this.presetId = presetId;
this.level = level;
//this.controls = controls == null ? config.controls[0].values : controls;
this.controls = controls == null ? [] : controls;
if (state == null) {
@@ -117,10 +113,6 @@ class GameState {
return configBundle.get(type);
}
private function get_levelId():LevelId {
return level == null ? 0 : level.id;
}
public function getTeamLife(id:TeamId):Int {
if (teams.exists(id)) {
return teams[id].life + Lambda.fold(teams[id].players, function(p, c) return c + p.life, 0);

View File

@@ -12,7 +12,7 @@ class GamePlayer extends Game {
private var ticker:Ticker;
public function new(record:GameRecord) {
super(record.state);
super(record.state, null);
this.record = record;
this.data = null;
this.ticker = new Ticker();

View File

@@ -17,11 +17,11 @@ class GameRecorder implements GameListener {
public function onGameEvent(event:GameEvent):Void {
switch event {
case GameEvent.START(state):
case GameEvent.START(state, level):
ticker.start();
record.info.type = state.type;
record.info.presetId = state.presetId;
record.info.levelId = state.levelId;
record.info.levelId = level.id;
record.info.date = Date.now();
case GameEvent.COMPLETE(_, _):
ticker.stop();

View File

@@ -1,5 +1,9 @@
package ru.m.tankz.util;
import haxe.io.Bytes;
import haxe.io.BytesInput;
import haxe.zip.Entry;
import haxe.zip.Reader;
import ru.m.tankz.config.Config;
import ru.m.tankz.Type;
import yaml.Parser;
@@ -65,4 +69,25 @@ class LevelUtil {
size: size,
}
}
private static function extract(entry:Entry):LevelConfig {
var bytes:Bytes = entry.data;
if (entry.compressed) {
#if ((flash || html5) && lime)
bytes = cast(bytes, lime.utils.Bytes).decompress(lime.utils.CompressionAlgorithm.DEFLATE);
#else
bytes = haxe.zip.Reader.unzip(entry);
#end
}
var level = LevelUtil.loads(bytes.toString());
if (level.id == null) {
level.id = Std.parseInt(entry.fileName.split("level").pop());
}
return level;
}
public static function unpack(bytes:Bytes):Array<LevelConfig> {
var files = Reader.readZip(new BytesInput(bytes));
return Lambda.array(files.map(extract));
}
}

View File

@@ -70,7 +70,7 @@ import ru.m.tankz.util.LevelUtil;
private function open():Void {
FileUtil.browse().then(function(content:FileContent) {
fileName = content.name;
var data = LevelUtil.loads(config, content.content);
var data = LevelUtil.loads(content.content);
mapView.gridSize = data.size;
mapView.data = data;
levelName.text = data.name;

View File

@@ -43,13 +43,13 @@ enum Brush {
override private function set_config(value:Config):Config {
var result = super.set_config(value);
builder = new EntityBuilder(value);
data = LevelUtil.empty(gridSize != null ? gridSize : config.map.grid, config.bricks[1]);
data = LevelUtil.empty(gridSize != null ? gridSize : config.map.grid, config.bricks[1].index);
return result;
}
override private function set_gridSize(value:GridSize):GridSize {
var result = super.set_gridSize(value);
data = LevelUtil.empty(gridSize != null ? gridSize : config.map.grid, config.bricks[1]);
data = LevelUtil.empty(gridSize != null ? gridSize : config.map.grid, config.bricks[1].index);
return result;
}
@@ -104,7 +104,7 @@ enum Brush {
private function get_data():LevelConfig {
return {
data: map.bricks.map(function(brick:Brick) return brick.config),
data: map.bricks.map(function(brick:Brick) return brick.config.index),
points: config.points,
size: gridSize,
}
@@ -114,7 +114,7 @@ enum Brush {
reset();
pointEntities = new Map();
map = new LevelMap(config.map, gridSize);
map.setData(value.data);
map.setData(value.data.map(function(index) return config.getBrickByIndex(index)));
gameEventSignal.emit(EventUtil.buildBricksSpawn(map));
for (point in config.points) {
switch point.type {

View File

@@ -13,7 +13,7 @@ class ServerConfigBundle implements IConfigBundle {
public function new() {}
public function get(type:GameType):Config {
var path:String = FileSystem.absolutePath('./resources/${type}/config.yaml');
var path:String = FileSystem.absolutePath('./resources/config/${type}.yaml');
var data:String = File.getContent(path);
var source:ConfigSource = Yaml.parse(data, Parser.options().useObjects());
return Config.fromSource(type, source);

View File

@@ -11,9 +11,13 @@ class ServerLevelBundle implements ILevelBundle {
public function new() {}
public function get(type:GameType, config:Config, levelId:LevelId):LevelConfig {
var path:String = FileSystem.absolutePath('./resources/${type}/levels/level${LevelUtil.formatLevel(levelId)}.txt');
var data:String = File.getContent(path);
return LevelUtil.loads(config, data);
public function get(type:GameType, name:String):LevelPack {
var path = FileSystem.absolutePath('./levels/${type}_${name}.zip');
var bytes = File.getBytes(path);
return {
type: type,
name: name,
data: LevelUtil.unpack(bytes),
};
}
}

View File

@@ -1,5 +1,6 @@
package ru.m.tankz.server.game;
import ru.m.tankz.bundle.ILevelBundle;
import ru.m.tankz.proto.room.RoomSlotProto;
import ru.m.tankz.config.Config;
import ru.m.tankz.core.EntityType;
@@ -18,8 +19,10 @@ class ServerGame extends GameRunner {
public var room(default, null):RoomProto;
public var id(get, null):Int;
@:provide static var levelBundle:ILevelBundle;
public function new(room:RoomProto) {
super(new GameState(room.game.type, 0, room.game.level));
super(new GameState(room.game.type, 0), levelBundle.get(room.game.type, "standard").data[room.game.level]);
this.controlFactory = new ServerControlFactory();
this.room = room;
}
@@ -84,7 +87,7 @@ class ServerGame extends GameRunner {
var result = [];
result.push(EventUtil.buildBricksSpawn(engine.map));
result = result.concat(EventUtil.buildCellsDestroyed(engine.map));
result.push(START(state));
result.push(START(state, level));
for (entity in engine.entities) {
switch EntityTypeResolver.of(entity) {
case EAGLE(eagle): result.push(EventUtil.buildEagleSpawn(eagle));