Commit 022672e7 by Shawn Wang

Merge branch 'develop' of cd.i.strikingly.com:shawn.wang/crazyflirt into develop

parents 6e9763d0 86015ab2
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
role: 'ghost', # runner role: 'ghost', # runner
x: 1, x: 1,
y: 3 y: 3
t_cid
} }
} }
......
class Controller extends egret.Sprite { class Controller extends egret.Sprite {
private myPosition = {}
public constructor() { public constructor() {
super(); super();
this.init() this.init()
...@@ -61,6 +62,17 @@ class Controller extends egret.Sprite { ...@@ -61,6 +62,17 @@ class Controller extends egret.Sprite {
controlContainer.addChild(this.down) controlContainer.addChild(this.down)
controlContainer.addChild(this.left) controlContainer.addChild(this.left)
controlContainer.addChild(this.right) controlContainer.addChild(this.right)
const me = Store.getState().runners.find(runner => runner.t_cid === Store.getState().cid)
this.myPosition.x = me.x
this.myPosition.y = me.y
EventBus.addEventListener('moving', (payload) => {
if (payload.target.payload.t_cid === Store.getState().cid) {
this.myPosition.x = payload.target.payload.x
this.myPosition.y = payload.target.payload.y
}
})
} }
private controlTouchBegin(e: egret.TouchEvent){ private controlTouchBegin(e: egret.TouchEvent){
...@@ -74,8 +86,22 @@ class Controller extends egret.Sprite { ...@@ -74,8 +86,22 @@ class Controller extends egret.Sprite {
}else if(e.currentTarget === this.right){ }else if(e.currentTarget === this.right){
delta.x += 1 delta.x += 1
} }
this.dispatchEventWith("position_change", false, delta)
platform.sendSocketMessage({
data: JSON.stringify({
type: 'moving',
room_id: 'default_room',
cid: Store.getState().cid,
payload: {
x: this.myPosition.x + delta.x,
y: this.myPosition.y + delta.y,
avatar_url: Store.getState().userInfo.avatarUrl,
nick_name: Store.getState().userInfo.nickName
}
}),
})
} }
public release() { public release() {
......
...@@ -90,7 +90,7 @@ class Main extends egret.DisplayObjectContainer { ...@@ -90,7 +90,7 @@ class Main extends egret.DisplayObjectContainer {
platform.sendSocketMessage({ platform.sendSocketMessage({
data: JSON.stringify({ data: JSON.stringify({
type: 'entry', type: 'entry',
rid: 'default_room', room_id: 'default_room',
payload: { payload: {
avatar_url: Store.getState().userInfo.avatarUrl, avatar_url: Store.getState().userInfo.avatarUrl,
nick_name: Store.getState().userInfo.nickName nick_name: Store.getState().userInfo.nickName
...@@ -108,24 +108,25 @@ class Main extends egret.DisplayObjectContainer { ...@@ -108,24 +108,25 @@ class Main extends egret.DisplayObjectContainer {
}) })
}) })
this.createGameScene(); this.createGameScene();
platform.onSocketMessage(function(res) {
platform.onSocketMessage(function (res) {
console.log('收到服务器内容:' + res.data) console.log('收到服务器内容:' + res.data)
const state = Store.onMessage(res.data)
let data = res.data let data = res.data
if (typeof res.data === 'string') { if (typeof res.data === 'string') {
data = JSON.parse(res.data) data = JSON.parse(res.data)
switch (data.type) { }
case 'entry': // data = { "type": "game_ready", "rid": "default_room", "payload": [{ "nick_name": "syfee", "avatar_url": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIibqBbRLAe0AI9NvnzPdLEBx9mT0ZKXicbibYWLAJiaPs0EZtmDiazJ7kytEj4tzWiberUptkR1yg0Q28g/132", "cid": "1525524303u0my3", "role": "ghost", "t_cid": "1525524303u0my3", x: 1, y: 1 }, { "nick_name": "syfee1", "avatar_url": "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIibqBbRLAe0AI9NvnzPdLEBx9mT0ZKXicbibYWLAJiaPs0EZtmDiazJ7kytEj4tzWiberUptkR1yg0Q28g/132", "cid": "1525524303u0my4", "role": "runner", "t_cid": "1525524303u0my4", x: 1, y: 1 }], "cid": "1525524303u0my4" }
Store.onMessage({ const state = Store.onMessage(data)
type: 'updateCid', EventBus.dispatch(data.type, data)
payload: data switch (data.type) {
}) case 'entry':
SceneManager.getInstance().roomScene.update(data) Store.onMessage({
break; type: 'updateCid',
case 'game_ready': payload: data
SceneManager.getInstance().roomScene.update(data) })
} SceneManager.getInstance().roomScene.update(data)
break;
case 'game_ready':
SceneManager.getInstance().roomScene.update(data)
} }
}) })
platform.onSocketError((e) => { platform.onSocketError((e) => {
...@@ -161,6 +162,8 @@ class Main extends egret.DisplayObjectContainer { ...@@ -161,6 +162,8 @@ class Main extends egret.DisplayObjectContainer {
*/ */
private createGameScene() { private createGameScene() {
this.addChild(SceneManager.getInstance()); this.addChild(SceneManager.getInstance());
// SoundManager.getInstance().play({soundName: 'bgm_mp3', loops: -1})
// SoundManager.getInstance().playCatched(1)
} }
/** /**
......
...@@ -5,7 +5,8 @@ const gridsInY = 10 ...@@ -5,7 +5,8 @@ const gridsInY = 10
class Map extends egret.Sprite { class Map extends egret.Sprite {
private grids:any = [] private grids:any = []
private touchArea: egret.Bitmap private touchArea: egret.Bitmap
private isShowingTouchArea public isShowingTouchArea
private runnerMap:any = {}
public constructor() { public constructor() {
super(); super();
this.init() this.init()
...@@ -28,6 +29,24 @@ class Map extends egret.Sprite { ...@@ -28,6 +29,24 @@ class Map extends egret.Sprite {
this.touchArea.texture = RES.getRes('mask_jpg') this.touchArea.texture = RES.getRes('mask_jpg')
this.touchArea.alpha = 0 this.touchArea.alpha = 0
this.addChild(this.touchArea) this.addChild(this.touchArea)
Store.getState().runners.forEach(runnerData => {
const runner = new Runner(runnerData)
this.addChild(runner)
this.runnerMap[runnerData.t_cid] = runner
})
EventBus.addEventListener('moving', (payload) => {
this.runnerMap[payload.target.payload.t_cid].x = payload.target.payload.x * gridWidth
this.runnerMap[payload.target.payload.t_cid].y = payload.target.payload.y * gridHeight
})
EventBus.addEventListener('clicking', (payload) => {
const x = payload.target.payload.x * gridWidth
const y = payload.target.payload.y * gridHeight
if (Store.isCatcher()) {
this.showSurroundedGrids(x, y)
} else {
this.showClickedGrid(x, y)
}
})
} }
public getSurroundedGrids(x, y) { public getSurroundedGrids(x, y) {
...@@ -44,13 +63,21 @@ class Map extends egret.Sprite { ...@@ -44,13 +63,21 @@ class Map extends egret.Sprite {
return grids return grids
} }
public showSurroundedGrids(x, y, centerGrid) { public showClickedGrid(x, y) {
if (this.isShowingTouchArea) { const surroundedGrids = this.getSurroundedGrids(x, y)
return
} const clickedGrid = surroundedGrids.find(grid => grid.x === x && grid.y === y)
clickedGrid.bg.texture = RES.getRes('grid_select_jpg')
setTimeout(() => {
clickedGrid.bg.texture = RES.getRes('grid_jpg')
}, 1000);
}
public showSurroundedGrids(x, y) {
this.isShowingTouchArea = true this.isShowingTouchArea = true
centerGrid.bg.texture = RES.getRes('grid_select_jpg')
const surroundedGrids = this.getSurroundedGrids(x, y) const surroundedGrids = this.getSurroundedGrids(x, y)
const centerGrid = surroundedGrids.find(grid => grid.x === x && grid.y === y)
centerGrid.bg.texture = RES.getRes('grid_select_jpg')
surroundedGrids.forEach(grid => { surroundedGrids.forEach(grid => {
grid.alpha = 1 grid.alpha = 1
}) })
......
...@@ -17,8 +17,20 @@ class MapGrid extends egret.Sprite { ...@@ -17,8 +17,20 @@ class MapGrid extends egret.Sprite {
} }
private onTouchTap() { private onTouchTap() {
if (Store.isCatcher()) { if (Store.isCatcher() && !this.parent.isShowingTouchArea) {
this.parent.showSurroundedGrids(this.x, this.y, this) platform.sendSocketMessage({
data: JSON.stringify({
type: 'clicking',
cid: Store.getState().cid,
room_id: 'default_room',
payload: {
x: this.x / 72,
y: this.y / 72,
avatar_url: Store.getMyInfo().avatarUrl,
nick_name: Store.getMyInfo().nickName
}
}),
})
} }
} }
......
class Guy extends egret.Sprite { class Runner extends egret.Sprite {
public constructor() { public constructor(data) {
super() super()
this.init() this.init(data)
} }
private avatar = new egret.Bitmap() private avatar = new egret.Bitmap()
private imageLoader = new egret.ImageLoader() private imageLoader = new egret.ImageLoader()
private init() { private init(data) {
this.avatar.width = 72 this.avatar.width = 72
this.avatar.height = 72 this.avatar.height = 72
this.x = data.x * this.avatar.width
this.y = data.y * this.avatar.height
this.imageLoader.addEventListener(egret.Event.COMPLETE,this.loadCompleteHandler,this) this.imageLoader.addEventListener(egret.Event.COMPLETE,this.loadCompleteHandler,this)
this.imageLoader.load("http://www.insajderi.com/wp-content/uploads/2018/01/asdja.jpg") this.imageLoader.load(data.avatar_url)
this.addChild(this.avatar) this.addChild(this.avatar)
} }
...@@ -39,4 +41,9 @@ class Guy extends egret.Sprite { ...@@ -39,4 +41,9 @@ class Guy extends egret.Sprite {
this.x = 72*13 + 10 this.x = 72*13 + 10
} }
} }
public moveTo(x, y) {
this.x = x
this.y = y
}
} }
\ No newline at end of file
...@@ -5,6 +5,10 @@ class SceneManager extends egret.DisplayObjectContainer { ...@@ -5,6 +5,10 @@ class SceneManager extends egret.DisplayObjectContainer {
public roomScene: RoomScene; public roomScene: RoomScene;
// 游戏场景 // 游戏场景
public gameScene: GameScene; public gameScene: GameScene;
private sceneMap: any = {
roomScene: RoomScene,
gameScene: GameScene
}
public constructor() { public constructor() {
super(); super();
...@@ -13,9 +17,9 @@ class SceneManager extends egret.DisplayObjectContainer { ...@@ -13,9 +17,9 @@ class SceneManager extends egret.DisplayObjectContainer {
private init() { private init() {
// 实例化两个场景 // 实例化两个场景
this.roomScene = new RoomScene(); this.roomScene = new RoomScene();
this.gameScene = new GameScene(); // this.gameScene = new GameScene();
// 默认添加开始场景 // 默认添加开始场景
this.addChild(this.gameScene); this.addChild(this.roomScene);
} }
// 实例化单例获取方法 // 实例化单例获取方法
public static getInstance(): SceneManager { public static getInstance(): SceneManager {
...@@ -26,10 +30,11 @@ class SceneManager extends egret.DisplayObjectContainer { ...@@ -26,10 +30,11 @@ class SceneManager extends egret.DisplayObjectContainer {
} }
// 切换场景 // 切换场景
public changeScene(type) { public changeScene(type) {
// 释放资源 if (!this[type]) {
if (this[type] && typeof this[type].release === 'function') { const Scene = this.sceneMap[type]
this[type].release() this[type] = new Scene ()
} }
// 移除所有显示列表中的对象 // 移除所有显示列表中的对象
this.removeChildren(); this.removeChildren();
// 添加下一个场景 // 添加下一个场景
......
class SoundManager {
private static instance: SoundManager;
private sounds: any
public constructor() {
this.init()
}
private init() {
this.sounds = {}
const soundNames = [
'catched_0_mp3',
'catched_1_mp3',
'catched_2_mp3',
'flirt_mp3',
'bgm_mp3',
]
soundNames.forEach(soundName => {
this.sounds[soundName] = RES.getRes(soundName)
})
}
// 实例化单例获取方法
public static getInstance(): SoundManager {
if (!SoundManager.instance) {
SoundManager.instance = new SoundManager();
}
return SoundManager.instance;
}
public play({soundName, startTime = 0, loops = 1}) {
this.sounds[soundName].play(startTime, loops)
}
// 随机播放被抓音频
public playCatched(num?) {
const soundName = [
'catched_0_mp3',
'catched_2_mp3',
][num || this.rnd(0, 1)]
this.play({soundName})
}
rnd(start, end) {
return Math.floor(Math.random() * (end - start) + start)
}
}
\ No newline at end of file
...@@ -2,25 +2,30 @@ let _state: any = { ...@@ -2,25 +2,30 @@ let _state: any = {
role: null, role: null,
cid: null, cid: null,
roomId: null, roomId: null,
userInfo: {},
runners: [],
catcher: {}
} }
const listenners = []
class Store { class Store {
static getState() { static getState() {
return _state return _state
} }
static onMessage(message) { static onMessage(message) {
const { payload } = message const { payload, room_id } = message
switch (message.type) { switch (message.type) {
case 'game_ready': case 'game_ready': {
const { cid, room_id, payload: { role } } = payload const runners = payload.filter(runner => runner.role !== 'ghost')
_state = { _state = {
..._state, ..._state,
cid,
role,
roomId: room_id, roomId: room_id,
runners,
} }
break; break;
}
case 'setUserInfo': case 'setUserInfo':
_state = { _state = {
..._state, ..._state,
...@@ -32,6 +37,7 @@ class Store { ...@@ -32,6 +37,7 @@ class Store {
..._state, ..._state,
cid: payload.cid cid: payload.cid
} }
break; break;
default: default:
break; break;
...@@ -39,8 +45,19 @@ class Store { ...@@ -39,8 +45,19 @@ class Store {
return _state return _state
} }
static subscribe(fn) {
}
static getMyInfo() {
return _state.userInfo
}
static getMyPosition
static isCatcher() { static isCatcher() {
return true console.log('isCatcher_____', _state.runners, _state.cid)
// return _state.role === 'ghost' return !_state.runners.some(runner => runner.t_cid === _state.cid)
} }
} }
\ No newline at end of file
window.Store = Store
\ No newline at end of file
...@@ -7,8 +7,6 @@ class GameScene extends egret.Sprite { ...@@ -7,8 +7,6 @@ class GameScene extends egret.Sprite {
this.init() this.init()
} }
private me = new Guy()
// 初始化(给开始按钮绑定点击事件) // 初始化(给开始按钮绑定点击事件)
private init() { private init() {
// put display object to this scene // put display object to this scene
...@@ -18,21 +16,15 @@ class GameScene extends egret.Sprite { ...@@ -18,21 +16,15 @@ class GameScene extends egret.Sprite {
bg.graphics.drawRect(0,0,1334, 750) bg.graphics.drawRect(0,0,1334, 750)
bg.graphics.endFill() bg.graphics.endFill()
this.addChild(bg) this.addChild(bg)
if (!Store.isCatcher()) {
this.controller = new Controller() this.controller = new Controller()
this.addChild(this.controller) this.addChild(this.controller)
this.controller.addEventListener("position_change", this.onPositionChange, this)
}
this.map = new Map() this.map = new Map()
this.map.y = 15 this.map.y = 15
this.map.x = 10 this.map.x = 10
this.me.y = 15
this.me.x = 10
this.addChild(this.map) this.addChild(this.map)
this.map.addChild(this.me)
}
private onPositionChange(delta){
this.me.applyDelta(delta.data)
} }
public release() { public release() {
......
class EventBusClass {
private listeners: any = {}
public addEventListener(type, callback, scope?) {
var args = [];
var numOfArgs = arguments.length;
for(var i=0; i<numOfArgs; i++){
args.push(arguments[i]);
}
args = args.length > 3 ? args.splice(3, args.length-1) : [];
if(typeof this.listeners[type] != "undefined") {
this.listeners[type].push({scope:scope, callback:callback, args:args});
} else {
this.listeners[type] = [{scope:scope, callback:callback, args:args}];
}
}
public removeEventListener(type, callback, scope) {
if(typeof this.listeners[type] != "undefined") {
var numOfCallbacks = this.listeners[type].length;
var newArray = [];
for(var i=0; i<numOfCallbacks; i++) {
var listener = this.listeners[type][i];
if(listener.scope == scope && listener.callback == callback) {
} else {
newArray.push(listener);
}
}
this.listeners[type] = newArray;
}
}
public hasEventListener(type, callback, scope) {
if(typeof this.listeners[type] != "undefined") {
var numOfCallbacks = this.listeners[type].length;
if(callback === undefined && scope === undefined){
return numOfCallbacks > 0;
}
for(var i=0; i<numOfCallbacks; i++) {
var listener = this.listeners[type][i];
if((scope ? listener.scope == scope : true) && listener.callback == callback) {
return true;
}
}
}
return false;
}
public dispatch(type, target) {
var event = {
type: type,
target: target
};
var args = [];
var numOfArgs = arguments.length;
for(var i=0; i<numOfArgs; i++){
args.push(arguments[i]);
};
args = args.length > 2 ? args.splice(2, args.length-1) : [];
args = [event].concat(args);
if(typeof this.listeners[type] != "undefined") {
var listeners = this.listeners[type].slice();
var numOfCallbacks = listeners.length;
for(var i=0; i<numOfCallbacks; i++) {
var listener = listeners[i];
if(listener && listener.callback) {
var concatArgs = args.concat(listener.args);
listener.callback.apply(listener.scope, concatArgs);
}
}
}
}
public getEvents() {
var str = "";
for(var type in this.listeners) {
var numOfCallbacks = this.listeners[type].length;
for(var i=0; i<numOfCallbacks; i++) {
var listener = this.listeners[type][i];
str += listener.scope && listener.scope.className ? listener.scope.className : "anonymous";
str += " listen for '" + type + "'\n";
}
}
return str;
}
}
if (!window.EventBus) {
window.EventBus = new EventBusClass()
}
declare let EventBus: EventBusClass;
declare interface Window {
EventBus: EventBusClass
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment