Initial prompt
MazZero
User prompt
Add these globals : // Game state variables var GAME_STATE = { INIT: 'INIT', MENU: 'MENU', NEW_ROUND: 'NEW_ROUND', PLAYING: 'PLAYING', SCORE: 'SCORE' }; var currentState = GAME_STATE.INIT;
User prompt
add initializeGame function and move all elements initialzation inside
User prompt
add initializeGame function and move all elements initialzation inside. all elements should be kept as global vairables. no locals in initializeGame
User prompt
remove duplicate
User prompt
move event handlers out of inifunction under a cmment /**** * Event Handlers ****/
User prompt
for each game state create 3 functions:initXXXState, handleXXXLoop and cleanXXXState
User prompt
add a new global debug = true and a new utility function function log() { if (debug) { console.log.apply(console, arguments); } }
User prompt
replace console.log by log() except in log() funciton ;)
User prompt
fix initXXXState, handleXXXLoop and cleanXXXState naming by using camel case (ie: initMenuState not initMENUState, ...)
Code edit (1 edits merged)
Please save this source code
User prompt
add and implement changeGameState(newState) (add detailed logs using log())
Code edit (1 edits merged)
Please save this source code
User prompt
in initializeGame call // Transition to menu state changeGameState(GAME_STATE.MENU);
Code edit (1 edits merged)
Please save this source code
User prompt
add global isPlaying = false
Code edit (1 edits merged)
Please save this source code
User prompt
add a new startButton asset and class; initialze it in game initialze and but show it only in menu state
User prompt
Please fix the bug: 'startButton is not defined' in or related to this line: 'startButton.visible = true;' Line Number: 146
Code edit (6 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
move puzzleManager.initPuzzle(); in initNewRound
User prompt
add 3 global containers layers
User prompt
add 3 global containers layers for background, middle and foreground
User prompt
Initialization of globals shoud come in initialzeGame
===================================================================
--- original.js
+++ change.js
@@ -1,191 +1,288 @@
-/****
+/****
* Classes
-****/
+****/
// HexTile class to represent each tile on the board
var HexTile = Container.expand(function (value, col, row) {
- var self = Container.call(this);
- // Properties
- self.value = value;
- self.originalValue = value;
- self.col = col;
- self.row = row;
- self.neighbors = [];
- // Asset Attachments
- var tileGraphics = self.attachAsset('hexTile', {
- anchorX: 0.5,
- anchorY: 0.5,
- x: col * 100,
- y: row * 100
- });
- var valueText = self.attachAsset('valueText', {
- anchorX: 0.5,
- anchorY: 0.5,
- x: 0,
- y: 0,
- text: value.toString()
- });
- // Functions
- self.setValue = function (newValue) {
- self.value = newValue;
- valueText.setText(newValue.toString());
- };
- self.playOperationEffect = function () {
- // Play operation animation
- };
- return self;
+ var self = Container.call(this);
+ // Properties
+ self.value = value;
+ self.originalValue = value;
+ self.col = col;
+ self.row = row;
+ self.neighbors = [];
+ // Asset Attachments
+ var tileGraphics = self.attachAsset('hexTile', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: col * 100,
+ y: row * 100
+ });
+ var valueText = self.attachAsset('valueText', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 0,
+ y: 0,
+ text: value.toString()
+ });
+ // Functions
+ self.setValue = function (newValue) {
+ self.value = newValue;
+ valueText.setText(newValue.toString());
+ };
+ self.playOperationEffect = function () {
+ // Play operation animation
+ };
+ return self;
});
// OperationButton class to represent each operation button
var OperationButton = Container.expand(function (type, uses) {
- var self = Container.call(this);
- // Properties
- self.type = type;
- self.remainingUses = uses;
- // Asset Attachments
- var buttonGraphics = self.attachAsset('operationButton', {
- anchorX: 0.5,
- anchorY: 0.5,
- x: 0,
- y: 0
- });
- var usesText = self.attachAsset('usesText', {
- anchorX: 0.5,
- anchorY: 0.5,
- x: 0,
- y: 30,
- text: uses.toString()
- });
- // Functions
- self.updateUses = function () {
- usesText.setText(self.remainingUses.toString());
- };
- return self;
+ var self = Container.call(this);
+ // Properties
+ self.type = type;
+ self.remainingUses = uses;
+ // Asset Attachments
+ var buttonGraphics = self.attachAsset('operationButton', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 0,
+ y: 0
+ });
+ var usesText = self.attachAsset('usesText', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 0,
+ y: 30,
+ text: uses.toString()
+ });
+ // Functions
+ self.updateUses = function () {
+ usesText.setText(self.remainingUses.toString());
+ };
+ return self;
});
-/****
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000 //Init game with black background
+ backgroundColor: 0x000000 //Init game with black background
});
-/****
+/****
* Game Code
-****/
-// Initialize PuzzleManager
-//<Assets used in the game will automatically appear here>
-//<Write imports for supported plugins here>
+****/
// PuzzleManager class to manage the game logic
+//<Write imports for supported plugins here>
+//<Assets used in the game will automatically appear here>
+// Initialize PuzzleManager
+function _createForOfIteratorHelper(r, e) {
+ var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
+ if (!t) {
+ if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
+ t && (r = t);
+ var _n = 0,
+ F = function F() {};
+ return {
+ s: F,
+ n: function n() {
+ return _n >= r.length ? {
+ done: !0
+ } : {
+ done: !1,
+ value: r[_n++]
+ };
+ },
+ e: function e(r) {
+ throw r;
+ },
+ f: F
+ };
+ }
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
+ }
+ var o,
+ a = !0,
+ u = !1;
+ return {
+ s: function s() {
+ t = t.call(r);
+ },
+ n: function n() {
+ var r = t.next();
+ return a = r.done, r;
+ },
+ e: function e(r) {
+ u = !0, o = r;
+ },
+ f: function f() {
+ try {
+ a || null == t["return"] || t["return"]();
+ } finally {
+ if (u) {
+ throw o;
+ }
+ }
+ }
+ };
+}
+function _unsupportedIterableToArray(r, a) {
+ if (r) {
+ if ("string" == typeof r) {
+ return _arrayLikeToArray(r, a);
+ }
+ var t = {}.toString.call(r).slice(8, -1);
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
+ }
+}
+function _arrayLikeToArray(r, a) {
+ (null == a || a > r.length) && (a = r.length);
+ for (var e = 0, n = Array(a); e < a; e++) {
+ n[e] = r[e];
+ }
+ return n;
+}
+// Game state variables
+var GAME_STATE = {
+ INIT: 'INIT',
+ MENU: 'MENU',
+ NEW_ROUND: 'NEW_ROUND',
+ PLAYING: 'PLAYING',
+ SCORE: 'SCORE'
+};
+var currentState = GAME_STATE.INIT;
function PuzzleManager(game) {
- var self = this;
- // Properties
- self.game = game;
- self.currentLevel = 0;
- self.board = [];
- self.operations = [];
- self.isAnimating = false;
- self.selectedOperation = null;
- self.moveCount = 0;
- self.levelData = null;
- // Core Functions
- self.initPuzzle = function () {
- self.loadLevel(self.currentLevel);
- };
- self.reset = function () {
- self.loadLevel(self.currentLevel);
- };
- self.loadLevel = function (levelNumber) {
- // Load level configuration
- self.levelData = {
- initialTiles: [{
- value: 3,
- col: 0,
- row: 0
- }, {
- value: -2,
- col: 1,
- row: 0
- }, {
- value: 1,
- col: 0,
- row: 1
- }],
- operations: [{
- type: '+1',
- uses: 3
- }, {
- type: '-1',
- uses: 2
- }]
- };
- self.createBoard();
- self.createOperations();
- };
- self.createBoard = function () {
- self.board = [];
- for (var i = 0; i < self.levelData.initialTiles.length; i++) {
- var tileData = self.levelData.initialTiles[i];
- var tile = new HexTile(tileData.value, tileData.col, tileData.row);
- self.board.push(tile);
- self.game.addChild(tile);
- }
- };
- self.createOperations = function () {
- self.operations = [];
- for (var i = 0; i < self.levelData.operations.length; i++) {
- var opData = self.levelData.operations[i];
- var operation = new OperationButton(opData.type, opData.uses);
- self.operations.push(operation);
- self.game.addChild(operation);
- }
- };
- self.applyOperation = function (operation, tile) {
- if (operation.remainingUses > 0) {
- self.propagateOperation(operation, tile);
- operation.remainingUses--;
- operation.updateUses();
- self.checkWinCondition();
- }
- };
- self.propagateOperation = function (operation, startTile) {
- var visited = new Set();
- var toProcess = [startTile];
- var targetValue = startTile.value;
- while (toProcess.length > 0) {
- var currentTile = toProcess.pop();
- if (visited.has(currentTile)) continue;
- visited.add(currentTile);
- // Apply operation
- currentTile.setValue(currentTile.value + parseInt(operation.type));
- currentTile.playOperationEffect();
- // Check neighbors
- for (var neighbor of currentTile.neighbors) {
- if (neighbor.value === targetValue && !visited.has(neighbor)) {
- toProcess.push(neighbor);
- }
- }
- }
- };
- self.checkWinCondition = function () {
- for (var tile of self.board) {
- if (tile.value !== 0) return false;
- }
- self.playVictoryAnimation();
- return true;
- };
- // Animation Functions
- self.playVictoryAnimation = function () {
- // Play level complete animation
- };
+ var self = this;
+ // Properties
+ self.game = game;
+ self.currentLevel = 0;
+ self.board = [];
+ self.operations = [];
+ self.isAnimating = false;
+ self.selectedOperation = null;
+ self.moveCount = 0;
+ self.levelData = null;
+ // Core Functions
+ self.initPuzzle = function () {
+ self.loadLevel(self.currentLevel);
+ };
+ self.reset = function () {
+ self.loadLevel(self.currentLevel);
+ };
+ self.loadLevel = function (levelNumber) {
+ // Load level configuration
+ self.levelData = {
+ initialTiles: [{
+ value: 3,
+ col: 0,
+ row: 0
+ }, {
+ value: -2,
+ col: 1,
+ row: 0
+ }, {
+ value: 1,
+ col: 0,
+ row: 1
+ }],
+ operations: [{
+ type: '+1',
+ uses: 3
+ }, {
+ type: '-1',
+ uses: 2
+ }]
+ };
+ self.createBoard();
+ self.createOperations();
+ };
+ self.createBoard = function () {
+ self.board = [];
+ for (var i = 0; i < self.levelData.initialTiles.length; i++) {
+ var tileData = self.levelData.initialTiles[i];
+ var tile = new HexTile(tileData.value, tileData.col, tileData.row);
+ self.board.push(tile);
+ self.game.addChild(tile);
+ }
+ };
+ self.createOperations = function () {
+ self.operations = [];
+ for (var i = 0; i < self.levelData.operations.length; i++) {
+ var opData = self.levelData.operations[i];
+ var operation = new OperationButton(opData.type, opData.uses);
+ self.operations.push(operation);
+ self.game.addChild(operation);
+ }
+ };
+ self.applyOperation = function (operation, tile) {
+ if (operation.remainingUses > 0) {
+ self.propagateOperation(operation, tile);
+ operation.remainingUses--;
+ operation.updateUses();
+ self.checkWinCondition();
+ }
+ };
+ self.propagateOperation = function (operation, startTile) {
+ var visited = new Set();
+ var toProcess = [startTile];
+ var targetValue = startTile.value;
+ while (toProcess.length > 0) {
+ var currentTile = toProcess.pop();
+ if (visited.has(currentTile)) {
+ continue;
+ }
+ visited.add(currentTile);
+ // Apply operation
+ currentTile.setValue(currentTile.value + parseInt(operation.type));
+ currentTile.playOperationEffect();
+ // Check neighbors
+ var _iterator = _createForOfIteratorHelper(currentTile.neighbors),
+ _step;
+ try {
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
+ var neighbor = _step.value;
+ if (neighbor.value === targetValue && !visited.has(neighbor)) {
+ toProcess.push(neighbor);
+ }
+ }
+ } catch (err) {
+ _iterator.e(err);
+ } finally {
+ _iterator.f();
+ }
+ }
+ };
+ self.checkWinCondition = function () {
+ var _iterator2 = _createForOfIteratorHelper(self.board),
+ _step2;
+ try {
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
+ var tile = _step2.value;
+ if (tile.value !== 0) {
+ return false;
+ }
+ }
+ } catch (err) {
+ _iterator2.e(err);
+ } finally {
+ _iterator2.f();
+ }
+ self.playVictoryAnimation();
+ return true;
+ };
+ // Animation Functions
+ self.playVictoryAnimation = function () {
+ // Play level complete animation
+ };
}
var puzzleManager = new PuzzleManager(game);
puzzleManager.initPuzzle();
// Event Listeners
game.down = function (x, y, obj) {
- // Handle operation button drag start
+ // Handle operation button drag start
};
game.move = function (x, y, obj) {
- // Handle operation button drag movement
+ // Handle operation button drag movement
};
game.up = function (x, y, obj) {
- // Handle operation button drag end
+ // Handle operation button drag end
};
\ No newline at end of file
tick
Sound effect
tileEntrance
Sound effect
tileRemove
Sound effect
operationSelect
Sound effect
operationCancel
Sound effect
tileChangeValue
Sound effect
resetSound
Sound effect
levelFailed
Sound effect
menuLevelSelect
Sound effect
menuCellEnter
Sound effect
applause
Sound effect
bgMusic
Music
tada
Sound effect