/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Block = Container.expand(function (blockType, blockColor) {
var self = Container.call(this);
self.blockType = blockType || 'buildBlock';
self.blockColor = blockColor || 0x4CAF50;
self.isPlaced = false;
self.originalX = 0;
self.originalY = 0;
var blockGraphics = self.attachAsset(self.blockType, {
anchorX: 0.5,
anchorY: 0.5
});
if (self.blockType === 'moveBlock') {
self.moveDirection = 1;
self.moveSpeed = 2;
self.moveRange = 100;
}
self.update = function () {
if (isPlayMode && self.blockType === 'moveBlock' && self.isPlaced) {
self.x += self.moveSpeed * self.moveDirection;
if (Math.abs(self.x - self.originalX) > self.moveRange) {
self.moveDirection *= -1;
}
}
};
self.down = function (x, y, obj) {
if (!isPlayMode && !self.isPlaced) {
isDragging = true;
draggedBlock = self;
}
};
return self;
});
var PaletteButton = Container.expand(function (blockType, yPos) {
var self = Container.call(this);
self.blockType = blockType;
self.y = yPos;
var buttonBlock = self.attachAsset(blockType, {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 0.8,
scaleY: 0.8
});
self.down = function (x, y, obj) {
if (!isPlayMode) {
selectedBlockType = self.blockType;
// Visual feedback
tween(self, {
scaleX: 1.0,
scaleY: 1.0
}, {
duration: 200
});
tween(self, {
scaleX: 0.8,
scaleY: 0.8
}, {
duration: 200
});
}
};
return self;
});
var Player = Container.expand(function () {
var self = Container.call(this);
var playerGraphics = self.attachAsset('player', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5;
self.lastIntersecting = {};
self.update = function () {
if (isPlayMode) {
// Check collisions with placed blocks
for (var i = 0; i < placedBlocks.length; i++) {
var block = placedBlocks[i];
var currentIntersecting = self.intersects(block);
var wasIntersecting = self.lastIntersecting[i] || false;
if (!wasIntersecting && currentIntersecting) {
if (block.blockType === 'triggerBlock') {
LK.getSound('trigger').play();
LK.setScore(LK.getScore() + 10);
scoreText.setText(LK.getScore());
LK.effects.flashObject(block, 0xFFFF00, 500);
} else if (block.blockType === 'barrierBlock') {
// Push player back
var dx = self.x - block.x;
var dy = self.y - block.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 0) {
self.x += dx / distance * 10;
self.y += dy / distance * 10;
}
}
}
self.lastIntersecting[i] = currentIntersecting;
}
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// Game state variables
var isPlayMode = false;
var isDragging = false;
var draggedBlock = null;
var selectedBlockType = 'buildBlock';
var placedBlocks = [];
var player = null;
// UI elements
var palette = game.addChild(LK.getAsset('palette', {
anchorX: 0,
anchorY: 0,
x: 50,
y: 200
}));
var modeButton = game.addChild(LK.getAsset('modeButton', {
anchorX: 0.5,
anchorY: 0.5,
x: 125,
y: 100
}));
var modeText = new Text2('BUILD', {
size: 30,
fill: 0xFFFFFF
});
modeText.anchor.set(0.5, 0.5);
modeText.x = 125;
modeText.y = 100;
game.addChild(modeText);
var scoreText = new Text2('Score: 0', {
size: 40,
fill: 0x333333
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
var instructionText = new Text2('Drag blocks to build!', {
size: 35,
fill: 0x333333
});
instructionText.anchor.set(0.5, 0);
instructionText.y = 50;
LK.gui.top.addChild(instructionText);
// Create palette buttons
var buildButton = game.addChild(new PaletteButton('buildBlock', 250));
buildButton.x = 100;
var moveButton = game.addChild(new PaletteButton('moveBlock', 350));
moveButton.x = 100;
var triggerButton = game.addChild(new PaletteButton('triggerBlock', 450));
triggerButton.x = 100;
var barrierButton = game.addChild(new PaletteButton('barrierBlock', 550));
barrierButton.x = 100;
// Mode button functionality
modeButton.down = function (x, y, obj) {
isPlayMode = !isPlayMode;
if (isPlayMode) {
modeText.setText('PLAY');
instructionText.setText('Touch to move player!');
// Create player if not exists
if (!player) {
player = game.addChild(new Player());
player.x = 1000;
player.y = 1000;
}
// Set original positions for moving blocks
for (var i = 0; i < placedBlocks.length; i++) {
if (placedBlocks[i].blockType === 'moveBlock') {
placedBlocks[i].originalX = placedBlocks[i].x;
placedBlocks[i].originalY = placedBlocks[i].y;
}
}
} else {
modeText.setText('BUILD');
instructionText.setText('Drag blocks to build!');
}
};
// Grid snap function
function snapToGrid(value) {
return Math.round(value / 40) * 40;
}
// Game event handlers
game.move = function (x, y, obj) {
if (isPlayMode && player) {
player.x = x;
player.y = y;
} else if (isDragging && draggedBlock) {
draggedBlock.x = snapToGrid(x);
draggedBlock.y = snapToGrid(y);
}
};
game.down = function (x, y, obj) {
if (!isPlayMode && x > 200) {
// Don't place blocks on palette area
// Create new block
var newBlock = new Block(selectedBlockType);
newBlock.x = snapToGrid(x);
newBlock.y = snapToGrid(y);
newBlock.isPlaced = true;
placedBlocks.push(newBlock);
game.addChild(newBlock);
LK.getSound('place').play();
LK.setScore(LK.getScore() + 1);
scoreText.setText('Score: ' + LK.getScore());
}
};
game.up = function (x, y, obj) {
isDragging = false;
draggedBlock = null;
};
game.update = function () {
// Update score display
scoreText.setText('Score: ' + LK.getScore());
// Check win condition
if (LK.getScore() >= 100) {
LK.showYouWin();
}
}; ===================================================================
--- original.js
+++ change.js
@@ -1,6 +1,235 @@
-/****
+/****
+* Plugins
+****/
+var tween = LK.import("@upit/tween.v1");
+var storage = LK.import("@upit/storage.v1");
+
+/****
+* Classes
+****/
+var Block = Container.expand(function (blockType, blockColor) {
+ var self = Container.call(this);
+ self.blockType = blockType || 'buildBlock';
+ self.blockColor = blockColor || 0x4CAF50;
+ self.isPlaced = false;
+ self.originalX = 0;
+ self.originalY = 0;
+ var blockGraphics = self.attachAsset(self.blockType, {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ if (self.blockType === 'moveBlock') {
+ self.moveDirection = 1;
+ self.moveSpeed = 2;
+ self.moveRange = 100;
+ }
+ self.update = function () {
+ if (isPlayMode && self.blockType === 'moveBlock' && self.isPlaced) {
+ self.x += self.moveSpeed * self.moveDirection;
+ if (Math.abs(self.x - self.originalX) > self.moveRange) {
+ self.moveDirection *= -1;
+ }
+ }
+ };
+ self.down = function (x, y, obj) {
+ if (!isPlayMode && !self.isPlaced) {
+ isDragging = true;
+ draggedBlock = self;
+ }
+ };
+ return self;
+});
+var PaletteButton = Container.expand(function (blockType, yPos) {
+ var self = Container.call(this);
+ self.blockType = blockType;
+ self.y = yPos;
+ var buttonBlock = self.attachAsset(blockType, {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ scaleX: 0.8,
+ scaleY: 0.8
+ });
+ self.down = function (x, y, obj) {
+ if (!isPlayMode) {
+ selectedBlockType = self.blockType;
+ // Visual feedback
+ tween(self, {
+ scaleX: 1.0,
+ scaleY: 1.0
+ }, {
+ duration: 200
+ });
+ tween(self, {
+ scaleX: 0.8,
+ scaleY: 0.8
+ }, {
+ duration: 200
+ });
+ }
+ };
+ return self;
+});
+var Player = Container.expand(function () {
+ var self = Container.call(this);
+ var playerGraphics = self.attachAsset('player', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ self.speed = 5;
+ self.lastIntersecting = {};
+ self.update = function () {
+ if (isPlayMode) {
+ // Check collisions with placed blocks
+ for (var i = 0; i < placedBlocks.length; i++) {
+ var block = placedBlocks[i];
+ var currentIntersecting = self.intersects(block);
+ var wasIntersecting = self.lastIntersecting[i] || false;
+ if (!wasIntersecting && currentIntersecting) {
+ if (block.blockType === 'triggerBlock') {
+ LK.getSound('trigger').play();
+ LK.setScore(LK.getScore() + 10);
+ scoreText.setText(LK.getScore());
+ LK.effects.flashObject(block, 0xFFFF00, 500);
+ } else if (block.blockType === 'barrierBlock') {
+ // Push player back
+ var dx = self.x - block.x;
+ var dy = self.y - block.y;
+ var distance = Math.sqrt(dx * dx + dy * dy);
+ if (distance > 0) {
+ self.x += dx / distance * 10;
+ self.y += dy / distance * 10;
+ }
+ }
+ }
+ self.lastIntersecting[i] = currentIntersecting;
+ }
+ }
+ };
+ return self;
+});
+
+/****
* Initialize Game
-****/
+****/
var game = new LK.Game({
- backgroundColor: 0x000000
-});
\ No newline at end of file
+ backgroundColor: 0x87CEEB
+});
+
+/****
+* Game Code
+****/
+// Game state variables
+var isPlayMode = false;
+var isDragging = false;
+var draggedBlock = null;
+var selectedBlockType = 'buildBlock';
+var placedBlocks = [];
+var player = null;
+// UI elements
+var palette = game.addChild(LK.getAsset('palette', {
+ anchorX: 0,
+ anchorY: 0,
+ x: 50,
+ y: 200
+}));
+var modeButton = game.addChild(LK.getAsset('modeButton', {
+ anchorX: 0.5,
+ anchorY: 0.5,
+ x: 125,
+ y: 100
+}));
+var modeText = new Text2('BUILD', {
+ size: 30,
+ fill: 0xFFFFFF
+});
+modeText.anchor.set(0.5, 0.5);
+modeText.x = 125;
+modeText.y = 100;
+game.addChild(modeText);
+var scoreText = new Text2('Score: 0', {
+ size: 40,
+ fill: 0x333333
+});
+scoreText.anchor.set(0.5, 0);
+LK.gui.top.addChild(scoreText);
+var instructionText = new Text2('Drag blocks to build!', {
+ size: 35,
+ fill: 0x333333
+});
+instructionText.anchor.set(0.5, 0);
+instructionText.y = 50;
+LK.gui.top.addChild(instructionText);
+// Create palette buttons
+var buildButton = game.addChild(new PaletteButton('buildBlock', 250));
+buildButton.x = 100;
+var moveButton = game.addChild(new PaletteButton('moveBlock', 350));
+moveButton.x = 100;
+var triggerButton = game.addChild(new PaletteButton('triggerBlock', 450));
+triggerButton.x = 100;
+var barrierButton = game.addChild(new PaletteButton('barrierBlock', 550));
+barrierButton.x = 100;
+// Mode button functionality
+modeButton.down = function (x, y, obj) {
+ isPlayMode = !isPlayMode;
+ if (isPlayMode) {
+ modeText.setText('PLAY');
+ instructionText.setText('Touch to move player!');
+ // Create player if not exists
+ if (!player) {
+ player = game.addChild(new Player());
+ player.x = 1000;
+ player.y = 1000;
+ }
+ // Set original positions for moving blocks
+ for (var i = 0; i < placedBlocks.length; i++) {
+ if (placedBlocks[i].blockType === 'moveBlock') {
+ placedBlocks[i].originalX = placedBlocks[i].x;
+ placedBlocks[i].originalY = placedBlocks[i].y;
+ }
+ }
+ } else {
+ modeText.setText('BUILD');
+ instructionText.setText('Drag blocks to build!');
+ }
+};
+// Grid snap function
+function snapToGrid(value) {
+ return Math.round(value / 40) * 40;
+}
+// Game event handlers
+game.move = function (x, y, obj) {
+ if (isPlayMode && player) {
+ player.x = x;
+ player.y = y;
+ } else if (isDragging && draggedBlock) {
+ draggedBlock.x = snapToGrid(x);
+ draggedBlock.y = snapToGrid(y);
+ }
+};
+game.down = function (x, y, obj) {
+ if (!isPlayMode && x > 200) {
+ // Don't place blocks on palette area
+ // Create new block
+ var newBlock = new Block(selectedBlockType);
+ newBlock.x = snapToGrid(x);
+ newBlock.y = snapToGrid(y);
+ newBlock.isPlaced = true;
+ placedBlocks.push(newBlock);
+ game.addChild(newBlock);
+ LK.getSound('place').play();
+ LK.setScore(LK.getScore() + 1);
+ scoreText.setText('Score: ' + LK.getScore());
+ }
+};
+game.up = function (x, y, obj) {
+ isDragging = false;
+ draggedBlock = null;
+};
+game.update = function () {
+ // Update score display
+ scoreText.setText('Score: ' + LK.getScore());
+ // Check win condition
+ if (LK.getScore() >= 100) {
+ LK.showYouWin();
+ }
+};
\ No newline at end of file