Commit 2409c386 by Fred Gao

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

parents 4a6640d4 3563970a
node_modules/
.wing/
fe_wxgame/*
!fe_wxgame/platform.js
\ No newline at end of file
{
type: 'moving', # 'start' , 'entry', 'clicking'
cid: 'abcdefg',
room_id: 'asdfadfaf',
payload: {
open_id: 'asdfasdfa',
picture: 'https://xxx.com',
x: 1,
y: 4
}
}
{
type: 'game_ready',
cid: 'abcdefg',
room_id: 'asdfadfaf',
payload: {
open_id: 'asdfasdfa',
picture: 'https://xxx.com',
role: 'ghost', # runner
x: 1,
y: 3
}
}
{
type: 'game_end',
room_id: 'asdfadfaf',
payload:{
}
}
\ No newline at end of file
......@@ -21,25 +21,31 @@ module CrazyFlirt
end
class RandomRole
class RandomPicker
def initialize
@roles = %i(ghost runner runner runner)
def initialize(clients)
@client = clients
@x_max = 14
@y_max = 10
@rd = Random.new
end
def pick_ghost_index
size = @client.size - 1
@rd.rand(0..size)
end
def pick_role
size = @roles.size - 1
index = Random.new.rand(0..size)
role = @roles[index]
@roles.delete_at(index)
role
def pick_born_point
x = @rd.rand(0..@x_max)
y = @rd.rand(0..@y_max)
[x, y]
end
end
class Client
attr_reader :key
attr_accessor :role
attr_accessor :role, :room
def initialize(ws)
@ws = ws
......@@ -47,7 +53,6 @@ module CrazyFlirt
@role = nil
end
def msg(msg)
@ws.send(msg.to_json)
end
......@@ -64,13 +69,14 @@ module CrazyFlirt
attr_reader :clients
def initialize(name)
@clients = {}
@clients = []
@name = name
@status = :waiting
end
def set_client(client)
@clients[client.key] = client
@clients << client
client.size - 1
end
def start!
......@@ -78,7 +84,8 @@ module CrazyFlirt
end
def leave_client(key)
@clients.delete(key)
idx = @clients.find_index { |c| c.key == key}
@clients.delete_at(idx)
end
end
......@@ -87,26 +94,30 @@ module CrazyFlirt
def initialize
Faye::WebSocket.load_adapter('thin')
@default_room = "default_room"
@faye = Faye::WebSocket
@socket_pool = {}
@room_pool = {}
@room_pool[@default_room] = GameRoom.new(@default_room)
end
def call(env)
return unless @faye.websocket?(env)
ws = @faye.new(env)
client = ::CrazyFlirt::Client.new(ws)
@socket_pool[client.key] = client
ws.on :message do |event|
ws.send(event.data)
message = JSON.parse(event.data)
seq = message[:seq]
command(seq, message)
seq = message[:type]
command(seq, message, client)
end
ws.on :close do |event|
@socket_pool.delete(client.key)
client.room.leave_client(client.key)
client = nil
puts 'closed'
end
ws.rack_response
......@@ -118,10 +129,16 @@ module CrazyFlirt
send(command, *args)
end
def clicking
def start(msg)
room = @room_pool[msg['room_id']]
picker = RandomPicker.new(room.clients)
idx = picker.pick_ghost_index
ghost = room.clients[idx]
end
def clicking(msg)
room = @room_pool[msg['room_id']]
room.clients.each do |client|
next if client.key == msg
client.msg(msg)
end
end
......@@ -129,7 +146,17 @@ module CrazyFlirt
def moving(msg)
room = @room_pool[msg['room_id']]
room.clients.each do |client|
next if client.key == msg
client.msg(msg)
end
end
def entry(msg, client)
room = @room_pool[msg['room_id']]
seat_index = room.set_client(client)
client.room = room
msg['cid'] = client.key
msg['payload']['seat'] = seat_index
room.clients.each do |client|
client.msg(msg)
end
end
......@@ -146,4 +173,4 @@ module CrazyFlirt
end
end
Rack::Handler.get('thin').run(CrazyFlirt::SocketEngine.new, Host: '192.168.50.118', Port: 80)
\ No newline at end of file
Rack::Handler.get('thin').run(CrazyFlirt::SocketEngine.new, Host: '192.168.50.118', Port: 9090)
\ No newline at end of file
......@@ -3,7 +3,7 @@
"compilerVersion": "5.1.10",
"template": {},
"target": {
"current": "web"
"current": "wxgame"
},
"modules": [
{
......
class Controller extends egret.Sprite {
public constructor() {
super();
this.init()
}
private speed = 10
private speedX = 0
private speedY = 0
private p1:egret.Point = new egret.Point();
private p2:egret.Point = new egret.Point();
private control = new egret.Shape();
private pointtt = new egret.Shape();
private controlCenter = new egret.Point(1100, 450)
// 初始化(给开始按钮绑定点击事件)
private init() {
this.control.graphics.beginFill( 0xFF3866, 1);
this.control.graphics.drawCircle(1100, 450, 90);
this.control.graphics.endFill();
this.addChild( this.control );
this.pointtt.graphics.beginFill(0xFF0000, 1)
this.pointtt.graphics.drawCircle(1100, 450, 10)
this.pointtt.graphics.endFill()
this.addChild(this.pointtt)
this.touchEnabled = true
this.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.controlTouchBegin, this)
this.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.controlTouchMove, this)
this.addEventListener(egret.TouchEvent.TOUCH_END, this.controlTouchEnd, this)
this.addEventListener(egret.Event.ENTER_FRAME,this.onEnterFrame,this)
}
private onEnterFrame(e: egret.Event){
if(this.touchEnd){
return
}
this.pointtt.x += this.speedX
this.pointtt.y += this.speedY
}
private touchEnd = false
private controlTouchBegin(e: egret.TouchEvent){
this.touchEnd = false
}
private controlTouchMove(e: egret.TouchEvent){
var angle = Math.atan2(e.stageY - this.controlCenter.y, e.stageX - this.controlCenter.x )
// console.error(e)
this.speedX = Math.cos(angle)*this.speed;
this.speedY = Math.sin(angle)*this.speed;
// console.error(this.speedX)
// console.error(e.stageX)
}
private controlTouchEnd(e: egret.TouchEvent){
this.touchEnd = true
// this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
}
public release() {
// 移除事件
}
}
\ No newline at end of file
......@@ -29,8 +29,6 @@
class Main extends egret.DisplayObjectContainer {
public constructor() {
super();
this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
......@@ -57,20 +55,63 @@ class Main extends egret.DisplayObjectContainer {
this.runGame().catch(e => {
console.log(e);
})
}
private async runGame() {
await this.loadResource()
var stage = egret.MainContext.instance.stage;
stage.setContentSize(1334, 750);
this.createGameScene();
const result = await RES.getResAsync("description_json")
this.startAnimation(result);
await platform.login();
const userInfo = await platform.getUserInfo();
console.log(userInfo);
platform.connectSocket({
url: "ws://192.168.50.118:9090",
header: {
'content-type': 'application/json'
},
method: "GET",
success: (...args) => {
console.error("success")
console.error(args)
},
fail: (...args) => {
console.error("fail")
console.error(args)
}
})
platform.onSocketOpen(() => {
platform.sendSocketMessage({
data: JSON.stringify({
cid: "asnbduawh",
seq: "moving",
x: 100,
y: 123
}),
success: () => {
console.error("sendSocketMessage success")
},
fail: () => {
console.error("sendSocketMessage fail")
}
})
})
platform.onSocketMessage(function(res) {
console.log('收到服务器内容:' + res.data)
const state = Store.onMessage(res.data)
})
platform.onSocketError((e) => {
console.error("onSocketError")
console.error(e)
})
platform.onSocketClose((e) => {
console.error("onSocketClose")
console.error(e)
})
}
private async loadResource() {
......@@ -87,63 +128,14 @@ class Main extends egret.DisplayObjectContainer {
}
private textfield: egret.TextField;
private myPosition = new egret.Point(0, 0)
/**
* 创建游戏场景
* Create a game scene
*/
private createGameScene() {
let sky = this.createBitmapByName("bg_jpg");
this.addChild(sky);
let stageW = this.stage.stageWidth;
let stageH = this.stage.stageHeight;
sky.width = stageW;
sky.height = stageH;
let topMask = new egret.Shape();
topMask.graphics.beginFill(0x000000, 0.5);
topMask.graphics.drawRect(0, 0, stageW, 172);
topMask.graphics.endFill();
topMask.y = 33;
this.addChild(topMask);
let icon = this.createBitmapByName("egret_icon_png");
this.addChild(icon);
icon.x = 26;
icon.y = 33;
let line = new egret.Shape();
line.graphics.lineStyle(2, 0xffffff);
line.graphics.moveTo(0, 0);
line.graphics.lineTo(0, 117);
line.graphics.endFill();
line.x = 172;
line.y = 61;
this.addChild(line);
let colorLabel = new egret.TextField();
colorLabel.textColor = 0xffffff;
colorLabel.width = stageW - 172;
colorLabel.textAlign = "center";
colorLabel.text = "Hello Egret";
colorLabel.size = 24;
colorLabel.x = 172;
colorLabel.y = 80;
this.addChild(colorLabel);
let textfield = new egret.TextField();
this.addChild(textfield);
textfield.alpha = 0;
textfield.width = stageW - 172;
textfield.textAlign = egret.HorizontalAlign.CENTER;
textfield.size = 24;
textfield.textColor = 0xffffff;
textfield.x = 172;
textfield.y = 135;
this.textfield = textfield;
this.addChild(SceneManager.getInstance());
}
/**
......@@ -156,34 +148,5 @@ class Main extends egret.DisplayObjectContainer {
result.texture = texture;
return result;
}
/**
* 描述文件加载成功,开始播放动画
* Description file loading is successful, start to play the animation
*/
private startAnimation(result: string[]) {
let parser = new egret.HtmlTextParser();
let textflowArr = result.map(text => parser.parse(text));
let textfield = this.textfield;
let count = -1;
let change = () => {
count++;
if (count >= textflowArr.length) {
count = 0;
}
let textFlow = textflowArr[count];
// 切换描述内容
// Switch to described content
textfield.textFlow = textFlow;
let tw = egret.Tween.get(textfield);
tw.to({ "alpha": 1 }, 200);
tw.wait(2000);
tw.to({ "alpha": 0 }, 200);
tw.call(change, this);
};
change();
}
}
\ No newline at end of file
}
......@@ -10,15 +10,53 @@ declare interface Platform {
login(): Promise<any>
connectSocket(options): void
onSocketOpen(callback): void
onSocketClose(callback): void
onSocketMessage(callback): void
onSocketError(callback): void
sendSocketMessage(callback): void
request(options): void
}
class DebugPlatform implements Platform {
async getUserInfo() {
return { nickName: "username" }
}
async login() {
}
showToast(title){
}
connectSocket(options){
}
onSocketOpen(callback){
}
onSocketClose(callback){
}
onSocketMessage(options){
}
onSocketError(callback){
}
sendSocketMessage(callback){
}
request(){
}
}
......
class SceneManager extends egret.DisplayObjectContainer {
// 场景控制器的单例
private static instance: SceneManager;
// 开始场景
private roomScene: RoomScene;
// 游戏场景
private gameScene: GameScene;
public constructor() {
super();
this.init();
}
private init() {
// 实例化两个场景
this.roomScene = new RoomScene();
this.gameScene = new GameScene();
// 默认添加开始场景
this.addChild(this.gameScene);
}
// 实例化单例获取方法
public static getInstance(): SceneManager {
if (!SceneManager.instance) {
SceneManager.instance = new SceneManager();
}
return SceneManager.instance;
}
// 切换场景
public changeScene(type) {
// 释放资源
if (this[type] && typeof this[type].release === 'function') {
this[type].release()
}
// 移除所有显示列表中的对象
this.removeChildren();
// 添加下一个场景
this.addChild(this[type]);
}
}
\ No newline at end of file
const _state = {}
class Store {
static getState() {
return _state
}
static onMessage(message) {
switch (message.type) {
case 'start':
_state = {
}
break;
default:
break;
}
return _state
}
}
\ No newline at end of file
class GameScene extends egret.Sprite {
private controller
public constructor() {
super();
this.init()
}
// 初始化(给开始按钮绑定点击事件)
private init() {
// put display object to this scene
this.controller = new Controller()
this.addChild(this.controller)
}
public release() {
// 移除事件
}
}
\ No newline at end of file
{
type: 'moving', # 'start' , 'entry', 'clicking'
cid: 'abcdefg',
room_id: 'asdfadfaf',
payload: {
open_id: 'asdfasdfa',
picture: 'https://xxx.com',
x: 1,
y: 4
}
}
{
type: 'game_ready',
cid: 'abcdefg',
room_id: 'asdfadfaf',
payload: {
open_id: 'asdfasdfa',
picture: 'https://xxx.com',
role: 'ghost', # runner
}
}
GameScene
RunnerList
if isRunner
Controller
Map
if isCatcher
Map.mask = touchArea
\ No newline at end of file
class RoomScene extends egret.Sprite {
public constructor() {
super();
this.init()
}
// 初始化(给开始按钮绑定点击事件)
private init() {
console.log('RoomScene.init')
// put display object to this scene
}
public release() {
// 移除事件
}
}
\ No newline at end of file
......@@ -31,7 +31,19 @@ egret_native.egretStart = function () {
//以下为自动修改,请勿修改
//The following is automatically modified, please do not modify
//----auto option start----
//----auto option end----
entryClassName: "Main",
frameRate: 30,
scaleMode: "fixedWidth",
contentWidth: 640,
contentHeight: 1136,
showPaintRect: false,
showFPS: false,
fpsStyles: "x:0,y:0,size:12,textColor:0xffffff,bgAlpha:0.9",
showLog: false,
logFilter: "",
maxTouches: 2,
textureScaleFactor: 1
//----auto option end----
};
egret.native.NativePlayer.option = option;
......
/**
* 请在白鹭引擎的Main.ts中调用 platform.login() 方法调用至此处。
*/
class WxgamePlatform {
name = 'wxgame'
login() {
return new Promise((resolve, reject) => {
wx.login({
success: (res) => {
resolve(res)
}
})
})
}
getUserInfo() {
return new Promise((resolve, reject) => {
wx.getUserInfo({
withCredentials: true,
success: function (res) {
var userInfo = res.userInfo
var nickName = userInfo.nickName
var avatarUrl = userInfo.avatarUrl
var gender = userInfo.gender //性别 0:未知、1:男、2:女
var province = userInfo.province
var city = userInfo.city
var country = userInfo.country
resolve(userInfo);
}
})
})
}
connectSocket(options){
wx.connectSocket(options)
}
onSocketOpen(callback){
wx.onSocketOpen(callback)
}
onSocketClose(callback){
wx.onSocketClose(callback)
}
onSocketMessage(callback){
wx.onSocketMessage(callback)
}
onSocketError(callback){
wx.onSocketError(callback)
}
sendSocketMessage(options){
wx.sendSocketMessage(options)
}
request(options){
wx.request(options)
}
}
window.platform = new WxgamePlatform();
var gulp = require('gulp')
var run = require('gulp-run-command').default
gulp.task("build", run(["egret run ./fe"]));
gulp.task(`watch`, () => {
gulp.watch('./fe/**/**.*', [`build`])
})
{
"name": "fe",
"version": "1.0.0",
"description": "",
"main": "gulpfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"gulp": "^3.9.1",
"gulp-run-command": "0.0.9",
}
}
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