Commit 3b730de1 authored by Vũ Gia Vương's avatar Vũ Gia Vương

build game

parent fded70ae
...@@ -60,6 +60,7 @@ function endGameApi(user) { ...@@ -60,6 +60,7 @@ function endGameApi(user) {
gameLevel: user.towerNumber - 1, gameLevel: user.towerNumber - 1,
details details
}; };
console.log('END GAME');
const res = yield callApi(URL_END_GAME, 'POST', params); const res = yield callApi(URL_END_GAME, 'POST', params);
return res; return res;
} }
......
...@@ -19,7 +19,7 @@ class User { ...@@ -19,7 +19,7 @@ class User {
this.screenPos = new v2_1.default(0, 0); this.screenPos = new v2_1.default(0, 0);
this.position = new v2_1.default(0, 0); this.position = new v2_1.default(0, 0);
this.timeStart = 0; this.timeStart = 0;
this.isEndGame = true; this.isStartGame = true;
this.history = []; this.history = [];
this.token = token; this.token = token;
this.id = id; this.id = id;
...@@ -29,7 +29,7 @@ class User { ...@@ -29,7 +29,7 @@ class User {
this.score = 0; this.score = 0;
this.totalScore = 0; this.totalScore = 0;
this.combo = 0; this.combo = 0;
this.isEndGame = true; this.isStartGame = false;
this.history = []; this.history = [];
this.timeStart = 0; this.timeStart = 0;
this.towerNumber = 0; this.towerNumber = 0;
......
...@@ -13,13 +13,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) { ...@@ -13,13 +13,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.setupSocket = setupSocket; exports.setupSocket = setupSocket;
// import CONFIG from "./Config/config";
const User_1 = __importDefault(require("./Model/User")); const User_1 = __importDefault(require("./Model/User"));
const v2_1 = __importDefault(require("./Model/v2")); const v2_1 = __importDefault(require("./Model/v2"));
const networkCtrl_1 = require("./Controller/networkCtrl"); const networkCtrl_1 = require("./Controller/networkCtrl");
const users = new Map(); const users = new Map();
const Y_RADIO = 0.5560472; const Y_RADIO = 0.5560472;
const HALF_SIZE_TOWER = 56.43580423808985; const HALF_SIZE_TOWER = 56.43580423808985;
const INIT_SPEED = 300;
const A_POWER = 300;
const DT = 0.016;
const MAX_TOWER = 50;
const CONFIG = { const CONFIG = {
EVT: { EVT: {
START_GAME: '200', START_GAME: '200',
...@@ -63,11 +66,8 @@ function endGame(socket) { ...@@ -63,11 +66,8 @@ function endGame(socket) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
const user = getUserBySocket(socket); const user = getUserBySocket(socket);
yield (0, networkCtrl_1.endGameApi)(user);
user.reset(); user.reset();
if (user === null || user === void 0 ? void 0 : user.isEndGame) {
return;
}
(0, networkCtrl_1.endGameApi)(user);
} }
catch (error) { catch (error) {
console.log('error', error); console.log('error', error);
...@@ -76,7 +76,7 @@ function endGame(socket) { ...@@ -76,7 +76,7 @@ function endGame(socket) {
} }
function onDisconnect(socket) { function onDisconnect(socket) {
console.log(`🔴 Client disconnected: ${socket.id}`); console.log(`🔴 Client disconnected: ${socket.id}`);
endGame(socket); // endGame(socket);
} }
function getHistory(socket) { function getHistory(socket) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
...@@ -89,10 +89,12 @@ function startGame(socket, data) { ...@@ -89,10 +89,12 @@ function startGame(socket, data) {
try { try {
const user = getUserBySocket(socket); const user = getUserBySocket(socket);
const result = yield (0, networkCtrl_1.startGameApi)(data); const result = yield (0, networkCtrl_1.startGameApi)(data);
// const result = true; if (result && user.isStartGame) {
user.reset();
}
if (result) { if (result) {
socket.emit(CONFIG.EVT.REQUEST_START_GAME, true); socket.emit(CONFIG.EVT.REQUEST_START_GAME, true);
user.isEndGame = false; user.isStartGame = true;
addBlock(socket); addBlock(socket);
} }
else { else {
...@@ -109,8 +111,18 @@ function passTower(socket, data) { ...@@ -109,8 +111,18 @@ function passTower(socket, data) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
try { try {
const user = getUserBySocket(socket); const user = getUserBySocket(socket);
const xDistance_ = data.distance; let xDistance_ = 0;
const { direction, nextBlock, towerNumber, position } = user; const { direction, nextBlock, towerNumber, position } = user;
if (towerNumber > MAX_TOWER) {
let speed = INIT_SPEED;
for (let i = 0; i < data.step; i++) {
speed += (Math.min(towerNumber - MAX_TOWER, A_POWER) * DT);
xDistance_ += speed * DT;
}
}
else {
xDistance_ = data.step * INIT_SPEED * DT;
}
const point = new v2_1.default(xDistance_ * direction, xDistance_ * Y_RADIO); const point = new v2_1.default(xDistance_ * direction, xDistance_ * Y_RADIO);
const nextTowerPos = nextBlock; const nextTowerPos = nextBlock;
const distancePlayer2NextTower = nextTowerPos.clone().sub(position).mag(); const distancePlayer2NextTower = nextTowerPos.clone().sub(position).mag();
...@@ -131,12 +143,7 @@ function passTower(socket, data) { ...@@ -131,12 +143,7 @@ function passTower(socket, data) {
target = nextTowerPos.clone(); target = nextTowerPos.clone();
score = 2; score = 2;
} }
// target = nextTowerPos.clone();
// score = 2;
const heightJump = user.distance2Tower * 0.7; const heightJump = user.distance2Tower * 0.7;
if (score <= 0) {
user.isEndGame = true;
}
if (score == 2) { if (score == 2) {
user.combo = Math.min(user.combo + 1, 2); user.combo = Math.min(user.combo + 1, 2);
score *= user.combo; score *= user.combo;
...@@ -169,7 +176,6 @@ function passTower(socket, data) { ...@@ -169,7 +176,6 @@ function passTower(socket, data) {
socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history); socket.emit(CONFIG.EVT.REQUEST_HISTORY, user.history);
yield (0, networkCtrl_1.endGameApi)(user); yield (0, networkCtrl_1.endGameApi)(user);
user.reset(); user.reset();
console.log('END');
} }
} }
catch (error) { catch (error) {
......
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