| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- // main.js
- let levelSending = false;
- let gameOverSending = false;
- let lastExitGame=false;
- let lastLevelCompleted = 0;
- let lastGameOver = 0;
- runOnStartup(async runtime =>
- {
- runtime.addEventListener(
- "beforeprojectstart",
- () => OnBeforeProjectStart(runtime)
- );
- runtime.addEventListener("function", OnFunction);
- });
- async function OnBeforeProjectStart(runtime)
- {
- console.log("Construct3 Started");
- // Nhận dữ liệu từ MVC
- window.addEventListener(
- "message",
- (event) => HandleMessage(runtime, event)
- );
-
- // Tick
- runtime.addEventListener(
- "tick",
- () => Tick(runtime)
- );
- console.log("Add event success");
- // Báo MVC biết game đã sẵn sàng
- window.parent.postMessage(
- {
- type: "GAME_READY"
- },
- "*"
- );
- console.log("GAME_READY sent");
- }
- function HandleMessage(runtime, event)
- {
- console.log("MESSAGE RECEIVED");
- console.log(event.data);
- if (!event.data || !event.data.type)
- return;
- switch (event.data.type)
- {
- case "INIT_GAME":
- console.log("INIT_GAME received: "+ event.data);
- runtime.globalVars.gameType =
- event.data.gameType ?? "";
- runtime.globalVars.msisdn =
- event.data.msisdn ?? "";
- runtime.globalVars.token =
- event.data.token ?? "";
- runtime.globalVars.transId =
- event.data.transId ?? "";
- runtime.globalVars.root =
- event.data.root ?? "";
- console.log(
- "Game Init:",
- runtime.globalVars.gameType,
- runtime.globalVars.msisdn
- );
- break;
- }
- }
- function Tick(runtime)
- {
- // ===== LEVEL COMPLETE (CHỈ 1 LẦN) =====
- if (
- runtime.globalVars.levelCompleted === 1 &&
- lastLevelCompleted === 0
- )
- {
- lastLevelCompleted = 1;
- SaveLevelResult(runtime);
- }
- if (runtime.globalVars.levelCompleted === 0)
- {
- lastLevelCompleted = 0;
- }
- // ===== GAME OVER (CHỈ 1 LẦN) =====
- /*
- if (
- runtime.globalVars.gameOver === 1 &&
- lastGameOver === 0
- )
- {
- lastGameOver = 1;
- SaveGameOver(runtime);
- }
- if (runtime.globalVars.gameOver === 0)
- {
- lastGameOver = 0;
- }
- */
- // ===== Exit game =====
- if (
- runtime.globalVars.exitGame === 1 &&
- lastExitGame === 0
- )
- {
- lastExitGame = 1;
- SaveExitGame(runtime);
- }
- if (runtime.globalVars.exitGame === 0)
- {
- lastExitGame = 0;
- }
- }
- //chu ý là khi hoàn thành level cần set
- //Set levelCompleted = 1
- //Sau khi vao level moi can set lại
- //Set levelCompleted = 0
- //Set showPrizePopup = 0
- async function SaveLevelResult(runtime)
- {
- try
- {
- console.log("Calling SaveLevelResult...");
-
- const body = {
- token: runtime.globalVars.token,
- msisdn: runtime.globalVars.msisdn,
- transId: runtime.globalVars.transId,
- level: runtime.globalVars.cur_level,
- score: runtime.globalVars.score,
- gameType: runtime.globalVars.gameType,
- gameOver: 0
- };
- console.log("Request Body:", body);
- console.log("JSON:", JSON.stringify(body));
- const response = await fetch(runtime.globalVars.root+"/Game/SaveLevelResult",
- {
- method: "POST",
- headers: {
- "Content-Type": "application/json"
- },
- body: JSON.stringify(body)
- });
- const result = await response.json();
- console.log("Level Result:", result);
- // Ví dụ dữ liệu trả về
- runtime.globalVars.point =
- result.point ?? "";
- runtime.globalVars.data =
- result.data ?? "";
- runtime.globalVars.score =
- result.score ?? "";
- runtime.globalVars.transId =
- result.transId ?? "";
- runtime.globalVars.showPrizePopup = 1;
- console.log("Chuẩn bị gọi vào function trong event sheet:");
- //runtime.callFunction("Quangbh");
- console.log("Da goi goi vao function event sheet xong nhe!!!");
-
- }
- catch (error)
- {
- console.error("SaveLevelResult Error:", error);
- }
- finally
- {
- levelSending = false;
- }
- }
- // Khi muốn thoát game về MVC
- function SaveExitGame(runtime)
- {
- try
- {
- const form = document.createElement("form");
- form.method = "POST";
- form.action = runtime.globalVars.root + "/Game/ExitGame";
- // Thêm input hidden
- function add(name, value)
- {
- const input = document.createElement("input");
- input.type = "hidden";
- input.name = name;
- input.value = value ?? "";
- form.appendChild(input);
- }
- add("token", runtime.globalVars.token);
- add("msisdn", runtime.globalVars.msisdn);
- add("transId", runtime.globalVars.transId);
- add("level", runtime.globalVars.cur_level);
- add("score", runtime.globalVars.score);
- add("gameType", runtime.globalVars.gameType);
- add("gameOver", runtime.globalVars.gameOver);
- document.body.appendChild(form);
- // Submit form
- HTMLFormElement.prototype.submit.call(form);
- }
- catch (error)
- {
- console.error("SaveExitGame Error:", error);
- }
- }
- //Khi thua game chi can set
- //Set gameOver = 1
- async function SaveGameOver(runtime)
- {
- try
- {
- console.log("Calling GameOver...");
- const response = await fetch(
- runtime.globalVars.root+"/Game/GameOver",
- {
- method: "POST",
- headers:
- {
- "Content-Type": "application/json"
- },
- body: JSON.stringify(
- {
- token: runtime.globalVars.token,
- msisdn: runtime.globalVars.msisdn,
- transId: runtime.globalVars.transId,
- score: runtime.globalVars.score,
- gameType: runtime.globalVars.gameType
- })
- }
- );
- const result = await response.json();
- console.log("GameOver Result:", result);
- runtime.globalVars.finalPrize =
- result.finalPrize ?? "";
- runtime.globalVars.showGameOverPopup = 1;
- }
- catch (error)
- {
- console.error("GameOver Error:", error);
- }
- finally
- {
- gameOverSending = false;
- }
- }
- function OnFunction(e)
- {
- switch (e.name)
- {
- case "ShowMessage":
- ShowMessage(e.args[0]);
- break;
- case "AddScore":
- AddScore(e.args[0]);
- break;
- case "MovePlayer":
- MovePlayer(e.args[0], e.args[1]);
- break;
- }
- }
- //===================================
- // Hiện thông báo
- //===================================
- function ShowMessage(message)
- {
- console.log(message);
- }
- //===================================
- // Cộng điểm
- //===================================
- function AddScore(score)
- {
- runtime.globalVars.Score += score;
- console.log("Score:", runtime.globalVars.Score);
- }
- //===================================
- // Di chuyển Player
- //===================================
- function MovePlayer(offsetX, offsetY)
- {
- const player = runtime.objects.Player.getFirstInstance();
- if (!player)
- return;
- player.x += offsetX;
- player.y += offsetY;
- }
|