[map] load

This commit is contained in:
2018-01-06 22:01:27 +03:00
parent 6ae91246ef
commit 3d876e7ef2
16 changed files with 330 additions and 170 deletions

View File

@@ -1,5 +1,7 @@
package ru.m.tankz;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.text.Font;
import ru.m.tankz.view.frames.StartFrame;
import haxework.log.SocketLogger;
@@ -25,64 +27,70 @@ class MainView extends VGroupView implements ViewBuilder {}
class Client implements IConnectionHandler {
private static inline var TAG = "Tankz";
private static inline var TAG = "Tankz";
public static function main() {
L.push(new TraceLogger());
#if flash
L.push(new JSLogger());
#end
#if debug
L.push(new SocketLogger());
#end
Const.init();
L.d(TAG, "Debug: " + Const.DEBUG);
L.i(TAG, "Version: " + Const.VERSION);
L.i(TAG, "Build: " + Const.BUILD);
new Client();
}
private var view:MainView;
public function new() {
Provider.setFactory(IResources, Resources);
var font:Font = Font.enumerateFonts()[0];
Provider.get(IResources).text.put("font", "Bookman Old Style");
Provider.get(IResources).text.put("version", 'v${Const.VERSION} b${Const.BUILD}');
Provider.set(IPacketBuilder, new PacketBuilder());
#if flash
Provider.set(IConnection, new ru.m.core.connect.flash.FlashConnection("localhost", 5001));
#elseif html5
Provider.set(IConnection, new ru.m.core.connect.js.JsConnection("localhost", 5001));
#end
Provider.get(IConnection).handler.addListener(this);
view = new MainView();
Provider.set(IFrameSwitcher, view.switcher);
Root.bind(view);
//view.logout.onPress = this;
view.switcher.change(StartFrame.ID);
}
public function onPress(view:ButtonView):Void {
switch (view.id) {
case "logout":
Provider.get(IConnection).disconnect();
public static function main() {
L.push(new TraceLogger());
#if flash
L.push(new JSLogger());
#end
#if debug
L.push(new SocketLogger());
#end
Const.init();
L.d(TAG, "Debug: " + Const.DEBUG);
L.i(TAG, "Version: " + Const.VERSION);
L.i(TAG, "Build: " + Const.BUILD);
new Client();
}
}
public function onConnected():Void {}
public function onDisconnected():Void {
//view.switcher.change(AuthFrame.ID);
}
private var view:MainView;
public function onError(error:Dynamic):Void {
L.e(TAG, "", error);
//view.switcher.change(AuthFrame.ID);
}
public function new() {
Provider.setFactory(IResources, Resources);
var font:Font = Font.enumerateFonts()[0];
Provider.get(IResources).text.put("font", "Bookman Old Style");
Provider.get(IResources).text.put("version", 'v${Const.VERSION} b${Const.BUILD}');
Provider.set(IPacketBuilder, new PacketBuilder());
#if flash
Provider.set(IConnection, new ru.m.core.connect.flash.FlashConnection("localhost", 5001));
#elseif html5
Provider.set(IConnection, new ru.m.core.connect.js.JsConnection("localhost", 5001));
#end
Provider.get(IConnection).handler.addListener(this);
view = new MainView();
Provider.set(IFrameSwitcher, view.switcher);
Root.bind(view);
//view.logout.onPress = this;
view.switcher.change(StartFrame.ID);
view.content.stage.addEventListener(KeyboardEvent.KEY_UP, function(event:KeyboardEvent):Void {
if (event.keyCode == Keyboard.ESCAPE) {
view.switcher.change(StartFrame.ID);
}
});
}
public function onPress(view:ButtonView):Void {
switch (view.id) {
case "logout":
Provider.get(IConnection).disconnect();
}
}
public function onConnected():Void {}
public function onDisconnected():Void {
//view.switcher.change(AuthFrame.ID);
}
public function onError(error:Dynamic):Void {
L.e(TAG, "", error);
//view.switcher.change(AuthFrame.ID);
}
}

View File

@@ -22,7 +22,7 @@ class PlayerControl {
moveQueue = new Array<Int>();
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
Lib.current.stage.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
//Lib.current.stage.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
}
private function onKeyDown(event:KeyboardEvent):Void {

View File

@@ -5,4 +5,5 @@ import ru.m.tankz.engine.IEngine;
interface IRender extends IView {
public function draw(game:IEngine):Void;
public function reset():Void;
}

View File

@@ -1,5 +1,7 @@
package ru.m.tankz.render;
import ru.m.tankz.core.Entity;
import ru.m.tankz.map.Brick;
import ru.m.tankz.core.Direction;
import flash.geom.Matrix;
import openfl.Assets;
@@ -10,78 +12,188 @@ import flash.display.Graphics;
import haxework.gui.SpriteView;
import ru.m.tankz.engine.IEngine;
interface IState<T> {
public function update(object:T):Bool;
}
class BrickState implements IState<Brick> {
private var type:BrickType;
public function new() {}
public function update(object:Brick):Bool {
if (type != object.type) {
type = object.type;
return true;
}
return false;
}
}
class EntityState implements IState<Entity> {
private var x:Float;
private var y:Float;
public function new() {}
public function update(object:Entity):Bool {
if (x != object.x || y != object.y) {
x = object.x;
y = object.y;
return true;
}
return false;
}
}
class Render extends SpriteView implements IRender {
private var mapLayer:Sprite;
private var tankLayer:Sprite;
private static var GROUND_BRICKS:Array<BrickType> = [
BrickType.BRICK, BrickType.ARMOR, BrickType.WATER, BrickType.ACE
];
public function new() {
super();
mapLayer = new Sprite();
tankLayer = new Sprite();
contentAsSprite.addChild(mapLayer);
contentAsSprite.addChild(tankLayer);
}
private static var UP_BRICKS:Array<BrickType> = [
BrickType.BUSH
];
public function draw(game:IEngine):Void {
var mapWidth = game.map.gridWidth * game.map.cellWidth;
var mapHeight = game.map.gridHeight * game.map.cellHeight;
private var backgroundLayer:Sprite;
private var groundLayer:Sprite;
private var entryLayer:Sprite;
private var upLayer:Sprite;
if (contentSize) {
width = mapWidth;
height = mapHeight;
private var layersForUpdate:Map<Sprite, Bool>;
private var states:Map<String, IState<Dynamic>>;
public function new() {
super();
layersForUpdate = new Map();
backgroundLayer = new Sprite();
groundLayer = new Sprite();
entryLayer = new Sprite();
upLayer = new Sprite();
contentAsSprite.addChild(backgroundLayer);
contentAsSprite.addChild(groundLayer);
contentAsSprite.addChild(entryLayer);
contentAsSprite.addChild(upLayer);
reset();
}
var g:Graphics = mapLayer.graphics;
g.clear();
g.beginFill(0x00ff00);
g.drawRect(0, 0, mapWidth, mapHeight);
g.endFill();
g.lineStyle(1, 0x000000);
for (i in 0...game.map.gridWidth + 1) {
g.moveTo(i * game.map.cellWidth, 0);
g.lineTo(i * game.map.cellWidth, mapHeight);
private function invalidateLayers(game:IEngine):Void {
for (brick in game.map.bricks) {
if (!states.exists(brick.key)) {
states[brick.key] = new BrickState();
}
if (states.get(brick.key).update(brick)) {
layersForUpdate[groundLayer] = true;
layersForUpdate[upLayer] = true;
}
}
for (entry in game.mobileEntities) {
if (!states.exists(entry.key)) {
states[entry.key] = new EntityState();
}
if (states.get(entry.key).update(entry)) {
layersForUpdate[entryLayer] = true;
}
}
}
for (i in 0...game.map.gridHeight + 1) {
g.moveTo(0, i * game.map.cellHeight);
g.lineTo(mapWidth, i * game.map.cellHeight);
private function drawBackground(game:IEngine):Void {
var mapWidth = game.map.gridWidth * game.map.cellWidth;
var mapHeight = game.map.gridHeight * game.map.cellHeight;
if (layersForUpdate[backgroundLayer]) {
var g:Graphics = backgroundLayer.graphics;
g.clear();
g.beginFill(0x000000);
g.drawRect(0, 0, mapWidth, mapHeight);
g.endFill();
if (contentSize) {
width = mapWidth;
height = mapHeight;
}
layersForUpdate[backgroundLayer] = false;
}
}
g.lineStyle();
var g:Graphics = tankLayer.graphics;
g.clear();
for (e in game.mobileEntities) {
if (Std.is(e, Tank)) {
var m = new Matrix();
m.rotate(calcRotate(e.direction));
m.translate(e.x, e.y);
g.beginBitmapFill(Assets.getBitmapData("resources/images/tank/player/tank_p0_0-0.png"), m);
g.drawRect(e.x, e.y, e.width, e.height);
g.endFill();
} else if (Std.is(e, Bullet)) {
var m = new Matrix();
m.rotate(calcRotate(e.direction));
m.translate(e.x, e.y);
g.beginBitmapFill(Assets.getBitmapData("resources/images/bullet/bullet_0.png"), m);
g.drawRect(e.x, e.y, e.width, e.height);
g.endFill();
}
private function drawMap(game:IEngine):Void {
if (layersForUpdate[groundLayer] || layersForUpdate[upLayer]) {
groundLayer.graphics.clear();
upLayer.graphics.clear();
for (brick in game.map.bricks) {
var g:Graphics = null;
if (GROUND_BRICKS.indexOf(brick.type) > -1) {
g = groundLayer.graphics;
} else if (UP_BRICKS.indexOf(brick.type) > -1) {
g = upLayer.graphics;
}
if (g != null) {
g.beginFill(0x00ff00);
g.beginBitmapFill(Assets.getBitmapData('resources/images/map/map_${brick.type}.png'));
g.drawRect(
brick.cellX * game.map.cellWidth,
brick.cellY * game.map.cellHeight,
game.map.cellWidth,
game.map.cellHeight
);
g.endFill();
}
}
layersForUpdate[groundLayer] = false;
layersForUpdate[upLayer] = false;
}
}
}
private function calcRotate(direction:Direction):Float {
return (if (direction == Direction.RIGHT) {
0;
} else if (direction == Direction.LEFT) {
180;
} else if (direction == Direction.TOP) {
270;
} else if (direction == Direction.BOTTOM) {
90;
} else {
0;
}) * (Math.PI / 180);
}
public function drawEntries(game:IEngine):Void {
if (layersForUpdate[entryLayer]) {
var g:Graphics = entryLayer.graphics;
g.clear();
for (e in game.mobileEntities) {
if (Std.is(e, Tank)) {
var m = new Matrix();
m.rotate(calcRotate(e.direction));
m.translate(e.x, e.y);
g.beginBitmapFill(Assets.getBitmapData("resources/images/tank/player/tank_p0_0-0.png"), m);
g.drawRect(e.x, e.y, e.width, e.height);
g.endFill();
} else if (Std.is(e, Bullet)) {
var m = new Matrix();
m.rotate(calcRotate(e.direction));
m.translate(e.x, e.y);
g.beginBitmapFill(Assets.getBitmapData("resources/images/bullet/bullet_0.png"), m);
g.drawRect(e.x, e.y, e.width, e.height);
g.endFill();
}
}
layersForUpdate[entryLayer] = false;
}
}
public function draw(game:IEngine):Void {
invalidateLayers(game);
drawBackground(game);
drawMap(game);
drawEntries(game);
}
public function reset():Void {
states = new Map<String, IState<Dynamic>>();
layersForUpdate[backgroundLayer] = true;
}
private function calcRotate(direction:Direction):Float {
return (if (direction == Direction.RIGHT) {
0;
} else if (direction == Direction.LEFT) {
180;
} else if (direction == Direction.TOP) {
270;
} else if (direction == Direction.BOTTOM) {
90;
} else {
0;
}) * (Math.PI / 180);
}
}

View File

@@ -31,7 +31,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
public function onShow():Void {
var game:Game = Provider.get(Game);
engine.init(ConfigBundle.get(GameType.CLASSIC));
engine.init(ConfigBundle.get(GameType.CLASSIC, 0));
engine.initTanks(game.players);
content.addEventListener(Event.ENTER_FRAME, updateGame);
for (index in 0...game.players.length) {
@@ -46,6 +46,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand
Provider.get(IConnection).packetHandler.removeListener(this);
content.removeEventListener(Event.ENTER_FRAME, updateGame);
engine.clear();
render.reset();
}
private function updateGame(_):Void {