Commit 2a0faee5 authored by 18류지석's avatar 18류지석

Merge branch 'physics'

# Conflicts:
#	js/main.js
parents 52d911a2 f56d7b42
......@@ -27,7 +27,7 @@ CSVParsing.CSVParse = function(scene) {
CSVParsing.gradeArray.grade2.push(allRows[singleRow].trim());
} else if(grade==1) {
CSVParsing.gradeArray.grade1.push(allRows[singleRow].trim());
} else {
} else if(grade==0) {
CSVParsing.gradeArray.grade0.push(allRows[singleRow].trim());
}
}
......
......@@ -9,18 +9,19 @@ class WordObject
this.wordGrade = WordReader.getWordGrade(this.wordTyping);
this.wordWeight = WordReader.getWordWeight(this.wordGrade);
//console.log("wordTyping : " + this.wordTyping + '\n' + "wordGrade : " + this.wordGrade + '\n' + "wordWeight : " + this.wordWeight + '\n');
this.wordSpeed = 1;
this.wordSpeed = 0.5;
}
instantiate(scene)
instantiate(scene,lenRate)
{
let p = [{x : 3, y : 0.7}, {x : 20, y : 1.8}];
let scale = ((p[1].y - p[0].y) / (p[1].x - p[0].x)) * (this.wordWeight - p[0].x) + p[0].y;
let fontscale = 25;
var random = WordSpace.getSpawnPoint();
var random = WordSpace.getSpawnPoint(lenRate);
this.physicsObj = scene.physics.add.sprite(random.x, random.y, 'wordBgr' + this.wordGrade + '_' + Math.min(Math.max(2, this.wordText.length), 6))
.setMass(this.wordWeight)
.setScale(scale);
.setScale(scale)
.setBounce(0.5);
this.wordObj = scene.add.text(random.x, random.y, this.wordText,
{
......@@ -29,6 +30,7 @@ class WordObject
fontStyle: (this.wordWeight > 5 ? 'bold' : '')
}).setColor('#000000').setOrigin(0.5,0.5);
WordSpace.totalWeight += this.wordWeight;
WordSpace.totalWordNum += 1;
WordSpace.setGameOverTimer();
//console.log("Total weight : " + WordSpace.totalWeight);
}
......@@ -37,6 +39,7 @@ class WordObject
{
console.log(this.generationCode + ': ' + this.wordText + ' destroyed');
WordSpace.totalWeight -= this.wordWeight;
WordSpace.totalWordNum -= 1;
this.wordObj.destroy();
const groupIdx = WordSpace.wordGroup.findIndex(function(item) {return this.isEqualObject(item.generationCode)}, this);
if (groupIdx > -1) WordSpace.wordGroup.splice(groupIdx, 1);
......@@ -47,8 +50,15 @@ class WordObject
attract()
{
var dist = Phaser.Math.Distance.Between(this.physicsObj.x, this.physicsObj.y, WordSpace.gravityPoint.x, WordSpace.gravityPoint.y);
var angle = Phaser.Math.Angle.Between(this.physicsObj.x, this.physicsObj.y, WordSpace.gravityPoint.x, WordSpace.gravityPoint.y);
let gravityScale = 0.1;
let accel = {x: 0, y: 0};
let dist = Phaser.Math.Distance.Between(this.physicsObj.x, this.physicsObj.y, WordSpace.gravityPoint.x, WordSpace.gravityPoint.y);
let angle = Phaser.Math.Angle.Between(this.physicsObj.x, this.physicsObj.y, WordSpace.gravityPoint.x, WordSpace.gravityPoint.y);
accel.x += Math.pow(dist,2) * gravityScale * Math.cos(angle);
accel.y += Math.pow(dist,2) * gravityScale * Math.sin(angle);
this.physicsObj.setVelocity(dist * Math.cos(angle) * this.wordSpeed, dist * Math.sin(angle) * this.wordSpeed);
this.wordObj.setPosition(this.physicsObj.x, this.physicsObj.y);
this.physicsObj.setVelocity(dist * Math.cos(angle) * this.wordSpeed, dist * Math.sin(angle) * this.wordSpeed);
this.wordObj.setPosition(this.physicsObj.x, this.physicsObj.y);
}
......
......@@ -39,6 +39,7 @@ WordReader.getWordTyping = function(stringText)
var temp = 0;
for(var i = 0; i < stringText.length; i++)
{
if(stringText.charCodeAt(i) < parseInt('0xac00',16) || stringText.charCodeAt(i) > parseInt('0xd7af',16)) return -1;
temp += parseFloat(firstSound(stringText.charAt(i))) + middleSound(stringText.charAt(i)) + lastSound(stringText.charAt(i));
}
return temp;
......@@ -47,9 +48,10 @@ WordReader.getWordTyping = function(stringText)
//입력 받은 단어의 등급을 반환함
WordReader.getWordGrade = function(_wordTyping)
{
return 2 <= _wordTyping && _wordTyping < 7 ? 3 :
7 <= _wordTyping && _wordTyping < 12 ? 2 :
12 <= _wordTyping && _wordTyping < 17 ? 1 : 0;
return 4 <= _wordTyping && _wordTyping < 7 ? 3 :
7 <= _wordTyping && _wordTyping < 12 ? 2 :
12 <= _wordTyping && _wordTyping < 17 ? 1 :
17 <= _wordTyping && _wordTyping < 26 ? 0 : -1;
}
WordReader.getWordWeight = function(_wordGrade)
......
......@@ -8,6 +8,7 @@ WordSpace.isImageLoaded = false;
WordSpace.nextWordCode = 0;
WordSpace.totalWeight = 0; //현재 단어 무게 총합
WordSpace.totalWordNum = 0;
WordSpace.brainCapacity = 100; //수용 가능한 단어 무게 최대치
WordSpace.defeatTime = 5000;
WordSpace.gameOverTimer = null; //게임 오버 판정 타이머
......@@ -17,30 +18,120 @@ WordSpace.wordGroup = [];
WordSpace.wordForcedGroup = [];
WordSpace.wordPhysicsGroup = null;
WordSpace.GradeProb = [0.35, 0.6, 0.8];
WordSpace.Phase = {READY: 0, START: 1, MAIN: 2, MUSIC: 3};
WordSpace.CurrentPhase = WordSpace.Phase.READY;
WordSpace.PlayerTyping = 0;
WordSpace.PlayerTypingRate = 0;
WordSpace.WordSpawnDelay = 3000;
WordSpace.NameSpawnDelay = 3000;
WordSpace.NameSpawnReduce = 1000;
WordSpace.gravityPoint = {x: 640, y: 300};
WordSpace.getSpawnPoint = function()
WordSpace.getSpawnPoint = function(_lenRate)
{
let xLen = 600;
let yLen = 300;
let lenRate = 1;
if(typeof _lenRate == 'number') lenRate = _lenRate;
let xLen = 600 * lenRate;
let yLen = 300 * lenRate;
const angle = Math.random() * Math.PI * 2;
let _x = xLen * Math.cos(angle) + this.gravityPoint.x;
let _y = yLen * Math.sin(angle) + this.gravityPoint.y;
return {x:_x, y:_y};
}
WordSpace.spaceInitiate = function(scene)
{
let arr = [2, 1, 3, 3, 2, 2, 3, 3, 2, 3]
let lenRate = 1;
arr.forEach(function(element)
{
WordSpace.generateWord(scene, SelectWord.selectWord(element),'',lenRate);
lenRate += 0.2;
});
}
WordSpace.AdjustVarByPhase = function(typingRate, phase)
{
if(phase == WordSpace.Phase.READY)
{
}
else if(phase == WordSpace.Phase.START)
{
WordSpace.WordSpawnDelay = 3000;
WordSpace.NameSpawnDelay = 6000;
WordSpace.NameSpawnReduce = 1000;
WordSpace.GradeProb[0] = 0.35;
WordSpace.GradeProb[1] = 1 - 0.4 * typingRate;
WordSpace.GradeProb[2] = 1;
}
else if(phase == WordSpace.Phase.MAIN)
{
WordSpace.WordSpawnDelay = 3000 - typingRate * 1000;
WordSpace.NameSpawnDelay = 6000;
WordSpace.NameSpawnReduce = 1000;
WordSpace.GradeProb[0] = 0.4 - 0.4 * typingRate;
WordSpace.GradeProb[1] = 0.8 - 0.4 * typingRate;
WordSpace.GradeProb[2] = 1 - 0.2 * typingRate;
}
else if(phase == WordSpace.Phase.MUSIC)
{
WordSpace.WordSpawnDelay = 1500;
WordSpace.NameSpawnDelay = 4000;
WordSpace.NameSpawnReduce = 500;
WordSpace.GradeProb[0] = 0.2 - 0.2 * typingRate;
WordSpace.GradeProb[1] = 0.8 - 0.5 * typingRate;
WordSpace.GradeProb[2] = 0.9 - 0.2 * typingRate;
}
WordSpace.wordCycle.resetCycle(WordSpace.gameSceneForTest, WordSpace.WordSpawnDelay, WordSpace.wordCycle.currentCycle.getElapsed());
WordSpace.nameCycle.resetCycle(WordSpace.gameSceneForTest, WordSpace.NameSpawnDelay, WordSpace.nameCycle.currentCycle.getElapsed());
}
WordSpace.GetPhase = function()
{
//서버통신하셈~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//임시
WordSpace.CurrentPhase = WordSpace.Phase.START;
}
WordSpace.GetPlayerTypingRate = function()
{
//서버통신하셈~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//임시
WordSpace.PlayerTypingRate = 0.5;
}
WordSpace.attackGauge =
{
value: 0,
gradeColor: ['#111124','#EBB435','#A42FFF','#1D22EB','#83947F'],
setRect: function()
{
this.rectUI.setSize(20 * this.value, 11);
this.rectUI.setPosition(640 - 10 * this.value, 597);
let tmp = this.getAttackOption();
this.rectUI.setFillStyle(Phaser.Display.Color.HexStringToColor(this.gradeColor[tmp.wordGrade + 1]).color);
},
generate: function(scene)
{
console.log("created");
this.rectUI = scene.add.rectangle(640,600,0,11).setDepth(11);
},
add: function(plus)
{
if (this.value + plus > 11) this.value = 11;
else this.value += plus;
this.setRect();
this.text.setText('게이지: ' + this.value.toFixed(1));
},
sub: function(minus)
{
if (this.value - minus < 0) this.value = 0;
else this.value -= minus;
this.setRect();
this.text.setText('게이지: ' + this.value.toFixed(1));
},
resetValue: function() {this.value = 0;},
......@@ -59,6 +150,7 @@ WordSpace.attackGauge =
this.currentCycle = scene.time.addEvent(option);
this.text = scene.add.text(100,100,'게이지: ' + this.value.toFixed(1)).setDepth(10).setColor('#000000');
//this.rectUI.setColor(this.gradeColor[0]);
},
pauseCycle: function(bool) {this.currentCycle.paused = bool;},
// showValue: 아래쪽에 바의 길이로 게이지 표시, 색으로 게이지의 강도 표현
......@@ -72,20 +164,87 @@ WordSpace.attackGauge =
}
}
WordSpace.varAdjustCycle =
{
currentCycle: null,
resetCycle : function(scene, _delay)
{
this.delay = _delay;
var option =
{
delay: _delay,
callback: function()
{
//나중에는 메세지 분석해서 Phase랑 PlayerTypingRate 받겠지만 일단 이렇게 해둠
WordSpace.GetPhase();
WordSpace.GetPlayerTypingRate();
WordSpace.AdjustVarByPhase(WordSpace.PlayerTypingRate, WordSpace.CurrentPhase);
},
callbackScope: scene,
loop: true
};
if (this.currentCycle != null)
{
this.currentCycle = this.currentCycle.reset(option);
}
else
{
this.currentCycle = scene.time.addEvent(option);
}
}
}
WordSpace.nameCycle =
{
currentCycle: null,
resetCycle: function(scene, _delay, _startAt)
{
this.delay = _delay;
var option =
{
startAt: _startAt,
delay: _delay,
callback: function()
{
console.log("호패 on");
},
callbackScope: scene,
loop: true
};
if (this.currentCycle != null)
{
this.currentCycle = this.currentCycle.reset(option);
}
else
{
this.currentCycle = scene.time.addEvent(option);
}
}
}
WordSpace.genWordByProb = function(scene)
{
let wordRnd = Math.random();
let wordIdx = wordRnd < WordSpace.GradeProb[0] ? 3 :
wordRnd < WordSpace.GradeProb[1] ? 2 :
wordRnd < WordSpace.GradeProb[2] ? 1 : 0;
WordSpace.generateWord(scene, SelectWord.selectWord(wordIdx));
}
WordSpace.wordCycle =
{
delay: 0,
currentCycle: null,
resetCycle: function(scene, _delay)
resetCycle: function(scene, _delay, _startAt)
{
this.delay = _delay;
var option =
{
startAt: _startAt,
delay: _delay,
callback: function()
{
let wordIdx = Math.floor(Math.random() * 4);
WordSpace.generateWord(this, SelectWord.selectWord(wordIdx));
WordSpace.genWordByProb(scene);
},
callbackScope: scene,
loop: true
......@@ -117,26 +276,26 @@ WordSpace.loadImage = function(scene)
WordSpace.weightTextObjForTest = scene.add.text(100, 75, '뇌의 무게: (현재) 0 / 100 (전체)').setDepth(10).setColor('#000000');
}
WordSpace.generateWord = function(scene, wordText, grade)
WordSpace.generateWord = function(scene, wordText, grade, lenRate)
{
word = new WordObject(wordText);
if (typeof grade != 'undefined')
if (typeof grade == 'number')
{
word.wordGrade = grade;
word.wordWeight = WordReader.getWordWeight(grade);
}
word.instantiate(scene);
word.instantiate(scene, lenRate);
WordSpace.wordGroup.push(word);
WordSpace.wordForcedGroup.push(word);
word.physicsObj.topObj = word;
scene.physics.add.collider(word.physicsObj, WordSpace.wordPhysicsGroup, function(object1)
{
object1.topObj.wordSpeed = 0.1;
//object1.topObj.wordSpeed = 0.1;
object1.topObj.attract();
});
WordSpace.wordPhysicsGroup.add(word.physicsObj);
WordSpace.weightTextObjForTest.setText('뇌의 무게: (현재) '+WordSpace.totalWeight+' / 100 (전체)');
WordSpace.weightTextObjForTest.setText('뇌의 무게: (현재) '+WordSpace.totalWeight+' / '+ this.brainCapacity+' (전체)');
}
function gameOver()
......@@ -195,8 +354,15 @@ WordSpace.findWord = function(wordText)
default: console.log('[ERR] wrong grade of word'); break;
}
weightest.destroy();
WordSpace.nameCycle.resetCycle(WordSpace.gameSceneForTest, WordSpace.NameSpawnDelay, WordSpace.nameCycle.currentCycle.getElapsed() + WordSpace.NameSpawnReduce);
while(WordSpace.totalWordNum < 5)
{
WordSpace.genWordByProb(WordSpace.gameSceneForTest);
WordSpace.wordCycle.resetCycle(WordSpace.gameSceneForTest, WordSpace.WordSpawnDelay, 0);
}
}
else if (wordText === '공격' && WordSpace.attackGauge.value > 3) // 공격모드 진입.
else if (wordText === '공격' && WordSpace.attackGauge.value >= 3) // 공격모드 진입.
{
console.log('attack mode');
Input.attackOption = this.attackGauge.getAttackOption();
......
......@@ -22,4 +22,103 @@ socket.on('idSet', function(msg) // {str, num}
{
console.log(msg.str);
this.playerNum = msg.num;
});
\ No newline at end of file
});
//test
window.addEventListener("message", function(event)
{
var sub = 0;
if(event.data == "tick"){
if(window.lafcb)
if((new Date().getTime()/1000) - window.lafcb.started > 0.5){
window.lafcb.func(new Date().getTime()+16)
window.lafcb = null;
}
var i = window.timeouts.length;
while (i--) {
if(window.timeouts[i].ran){
window.timeouts.splice(i,1);
}
}
var i = window.timeouts.length;
while (i--) {
if(new Date().getTime() - window.timeouts[i].started >= window.timeouts[i].delay && window.timeouts[i]){
window.timeouts[i].func();
window.timeouts[i].ran = true;
}
}
for(var i in window.intervals){
var currTime = new Date().getTime();
if(currTime - window.intervals[i].last >= window.intervals[i].delay && window.intervals[i]){
window.intervals[i].last = currTime;
window.intervals[i].func();
}
}
window.postMessage('tick', '*');
}
}, false);
(function(context) {
'use strict';
window.lafcb = null;
context.timeouts = [];
context.intervals = [];
var lastTime = new Date().getTime();
context.old = {};
old.setTimeout = (i,ii)=> context.setTimeout(i,ii);
old.setInterval = (i,ii) =>context.setInterval(i,ii);
old.clearTimeout = (i) =>context.clearTimeout(i);
old.clearInterval = (i) =>context.clearInterval(i);
if(typeof(context.postMessage) == 'function'){
context.setTimeout = function(fn, millis) {
var id = timeouts.length
timeouts[id] = {id: id,func: fn, delay: millis,started: new Date().getTime()};
return id;
};
context.clearTimeout = function(cancel) {
for(var i in timeouts){
if(timeouts[i].id == cancel){
timeouts.splice(i,1);
break;
}
}
};
context.setInterval = function(fn, delay ) {
intervals[intervals.length] = {func: fn, delay: delay,last: new Date().getTime()};
return intervals[intervals.length-1];
};
context.clearInterval = function(cancel) {
for(var i in intervals){
if(intervals[i] == cancel){
intervals.splice(i,1);
break;
}
}
};
}
context.requestAnimationFrame = function( callback, element ) {
lafcb = {started: new Date().getTime()/1000,func: callback};
var currTime = new Date().getTime();
var timeToCall = 16;
var id = context.setTimeout( function() {
callback( currTime+timeToCall);
}, timeToCall );
return id;
};
context.cancelAnimationFrame = function( id ) {
lafcb
context.clearTimeout( id );
};
context.addEventListener("load",function(){
if(typeof(context.postMessage) == 'function'){
context.postMessage('tick', '*');
}else{
context.setTimeout = old.setTimeout
context.setInterval = old.setInterval
context.clearTimeout = old.clearTimeout
context.clearInterval = old.clearInterval
alert("Your browser does not support postMessage. Sorry but you will be forced to default to the standard setInterval and setTimeout functions. This means you may experience pauses in your game when you navigate away from the tab it is playing in.");
}
});
})(this);
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