스테이지가 지남에 따라서 점수가 오르게하는 로직
deltatime이 뭐길래 많이 나올까 생각했습니다.
deltatime은 지난 프레임 이후 경과한 시간 입니다.
js에서는 1s = 1000ms
로 받아들여져서 1프레임당 deltatime이 16.7이라고 가정했을때
60프레임이라고 할수있다.
// 0
// 0 += 16.77 * 0.001 = 0.01677
// 0.016 += 0.016
// 0.032 += 0.016
// 0.048 += 0.016
// .....
// 99.99 += 0.016 // 100이 넘을때 스테이지 넘어감
// 100.015 += 0.016 this.stageChange = true
// 100.031 += 0.016
이러한 구조를 가지고 있다.
stage 가 지남에 따라서 획득 점수 조정
// stage.json
{
"name": "stage",
"version": "1.0.0",
"data": [
{ "id": 1000, "score": 0, "scorePerSecond": 1 },
{ "id": 1001, "score": 50, "scorePerSecond": 2 },
{ "id": 1002, "score": 100, "scorePerSecond": 4 },
{ "id": 1003, "score": 200, "scorePerSecond": 8 },
{ "id": 1004, "score": 300, "scorePerSecond": 16 },
{ "id": 1005, "score": 500, "scorePerSecond": 32 },
{ "id": 1006, "score": 600, "scorePerSecond": 64 }
]
}
//Score.js
update(deltaTime) {
this.stageChange = true;
const stageData = gameAssetsData.stages.data;
this.score += deltaTime * 0.001 * stageData[this.stage].scorePerSecond; // deltaTime 프레임//0.001 = 1초 (1000/60 = 16.77)
console.log('delta', deltaTime);
if (
Math.floor(this.score) >= stageData[this.stage + 1].score &&
this.stageChange
) {
this.stageChange = false;
// this.stageChange = false
sendEvent(11, {
currentStage: stageData[this.stage].id,
targetStage: stageData[this.stage + 1].id,
});
this.stage++; // this.stage += 1;
}
stage =0;
gameAssets = { stages, items, itemUnlocks };
부분을 참고하여
stages를 assets.js의 gameAssetsData 로 불러주어
gameAssetsData.stages.data 형태로 stageData 변수를 지정해줍니다.
stage+1을 해준 이유는 stage는 0 에서부터 시작하기때문에 1을 더해줘야 합니다.
item획득시 점수 변화 로직
getItem(itemId) {
// 아이템 획득시 점수 변화
const itemData = gameAssetsData.items.data.find(
(item) => item.id === itemId,
);
this.score += itemData.score;
}
find함수로 item을 찾아오는 과정
아이템 한개정돈.. 수정해보자 싶어서 이미지 추가..!
완성본!