diff --git a/src/client/haxe/ru/m/tankz/Client.hx b/src/client/haxe/ru/m/tankz/Client.hx index 7bdfcbd..881cd68 100755 --- a/src/client/haxe/ru/m/tankz/Client.hx +++ b/src/client/haxe/ru/m/tankz/Client.hx @@ -1,5 +1,6 @@ package ru.m.tankz; +import ru.m.tankz.sound.SoundManager; import flash.events.KeyboardEvent; import flash.text.Font; import flash.ui.Keyboard; @@ -86,6 +87,7 @@ class Client implements IConnectionHandler { Provider.setFactory(IConfigBundle, ConfigBundle); Provider.setFactory(ILevelBundle, LevelBundle); Provider.setFactory(SaveStorage, SaveStorage); + Provider.setFactory(SoundManager, SoundManager); Provider.setFactory(Game, ClassicGame, ClassicGame.TYPE); Provider.setFactory(Game, DotaGame, DotaGame.TYPE); diff --git a/src/client/haxe/ru/m/tankz/frame/GameFrame.hx b/src/client/haxe/ru/m/tankz/frame/GameFrame.hx index de8a2d3..3b0e1c1 100755 --- a/src/client/haxe/ru/m/tankz/frame/GameFrame.hx +++ b/src/client/haxe/ru/m/tankz/frame/GameFrame.hx @@ -1,5 +1,6 @@ package ru.m.tankz.frame; +import ru.m.tankz.sound.SoundManager; import flash.events.Event; import haxe.ds.Option; import haxe.Timer; @@ -45,6 +46,7 @@ class GameFrame extends VGroupView implements ViewBuilder implements IPacketHand throw 'Unsupported game type "${save.state.type}"'; } game.engine.listeners.push(render); + game.engine.listeners.push(Provider.get(SoundManager)); game.start(save).then(onGameStateChange).endThen(onGameComplete); content.addEventListener(Event.ENTER_FRAME, redraw); //Provider.get(IConnection).packetHandler.addListener(this); diff --git a/src/client/haxe/ru/m/tankz/sound/SoundManager.hx b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx new file mode 100644 index 0000000..593b8b7 --- /dev/null +++ b/src/client/haxe/ru/m/tankz/sound/SoundManager.hx @@ -0,0 +1,59 @@ +package ru.m.tankz.sound; + +import ru.m.tankz.core.EntityType; +import ru.m.tankz.engine.Engine; +import flash.media.Sound; +import openfl.utils.Assets; + + +class SoundManager implements EngineListener { + private static var TAG(default, never):String = 'SoundManager'; + + public function new() {} + + public function play(id:String):Void { + L.d(TAG, 'player: ${id}'); + var sound:Sound = Assets.getSound('resources/sounds/${id}.mp3'); + sound.play(); + } + + public function onSpawn(entity:EntityType):Void { + switch entity { + case EntityType.BULLET(_.tank.playerId.team => 'human'): + play('shot'); + case EntityType.BONUS(_): + play('bonus_add'); + case _: + } + } + + public function onChange(entity:EntityType, ?change:EntityChange):Void { + switch [entity, change] { + case [EntityType.TANK(_), EntityChange.HIT]: + play('bullet_hit'); + case [EntityType.TANK(_), EntityChange.LIVE_UP]: + play('live'); + case [EntityType.EAGLE(_), EntityChange.DEATH]: + play('boom_player'); + case _: + } + } + + public function onCollision(entity:EntityType, with:EntityType):Void { + switch [entity, with] { + case [EntityType.BULLET(_), EntityType.CELL(cell)]: + //play('bullet_wall'); + case _: + } + } + + public function onDestroy(entity:EntityType):Void { + switch entity { + case EntityType.TANK(_): + play('boom_bot'); + case EntityType.BONUS(_): + play('bonus_get'); + case _: + } + } +} diff --git a/src/common/haxe/ru/m/tankz/core/Bullet.hx b/src/common/haxe/ru/m/tankz/core/Bullet.hx index f054185..a99fcbd 100644 --- a/src/common/haxe/ru/m/tankz/core/Bullet.hx +++ b/src/common/haxe/ru/m/tankz/core/Bullet.hx @@ -9,10 +9,12 @@ import ru.m.tankz.Type; class Bullet extends MobileEntity { public var playerId(default, null):PlayerId; public var tankId(default, null):Int; + public var tank(default, null):Tank; public var config(default, null):BulletConfig; public function new(tank:Tank) { this.playerId = tank.playerId; + this.tank = tank; this.config = tank.config.bullet; super(new Rectangle(0, 0, config.width, config.height), config.speed, Direction.RIGHT); this.tankId = tank.id; diff --git a/src/common/haxe/ru/m/tankz/engine/Engine.hx b/src/common/haxe/ru/m/tankz/engine/Engine.hx index c305395..9610dd4 100755 --- a/src/common/haxe/ru/m/tankz/engine/Engine.hx +++ b/src/common/haxe/ru/m/tankz/engine/Engine.hx @@ -15,6 +15,7 @@ import ru.m.tankz.map.LevelMap; enum EntityChange { DEATH; HIT; + LIVE_UP; }