/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
chaoStats: {
age: 0,
happiness: 50,
hunger: 50,
energy: 50,
lastTimestamp: 0
},
chaoEvolution: "baby",
chaoColor: 7399423,
gardenItems: []
});
/****
* Classes
****/
var Chao = Container.expand(function () {
var self = Container.call(this);
// Default chao stats
self.stats = storage.chaoStats || {
age: 0,
happiness: 50,
hunger: 50,
energy: 50,
lastTimestamp: Date.now()
};
self.evolution = storage.chaoEvolution || 'baby';
self.color = storage.chaoColor || 0x70e7ff;
// Create chao body parts
var body = self.attachAsset('chaoBody', {
anchorX: 0.5,
anchorY: 0.5,
y: 10
});
var head = self.attachAsset('chaoHead', {
anchorX: 0.5,
anchorY: 0.5,
y: -40
});
// Eyes
var leftEye = self.attachAsset('chaoEyes', {
anchorX: 0.5,
anchorY: 0.5,
x: -20,
y: -45
});
var rightEye = self.attachAsset('chaoEyes', {
anchorX: 0.5,
anchorY: 0.5,
x: 20,
y: -45
});
// Set color
body.tint = self.color;
head.tint = self.color;
// Movement properties
self.targetX = self.x;
self.targetY = self.y;
self.moveSpeed = 2;
self.idleTime = 0;
self.idleThreshold = 180; // 3 seconds at 60fps
self.isWalking = false;
// Timers
self.statDecayInterval = 600; // 10 seconds at 60fps
self.statDecayCounter = 0;
// Apply evolution appearance
self.updateEvolution = function () {
storage.chaoEvolution = self.evolution;
storage.chaoColor = self.color;
var scale = 0.7; // Baby scale
if (self.evolution === 'child') {
scale = 0.85;
} else if (self.evolution === 'adult') {
scale = 1.0;
}
tween(self, {
scaleX: scale,
scaleY: scale
}, {
duration: 1000,
easing: tween.easeOut
});
body.tint = self.color;
head.tint = self.color;
};
// Update Chao stats
self.updateStats = function () {
self.statDecayCounter++;
if (self.statDecayCounter >= self.statDecayInterval) {
self.statDecayCounter = 0;
// Slowly decrease stats over time
self.stats.hunger = Math.max(0, self.stats.hunger - 2);
self.stats.happiness = Math.max(0, self.stats.happiness - 1);
self.stats.energy = Math.max(0, self.stats.energy - 1);
// Update UI
updateStatsUI();
// Save stats
storage.chaoStats = self.stats;
}
// Check if should evolve
if (self.stats.age >= 20 && self.evolution === 'baby') {
self.evolution = 'child';
self.updateEvolution();
LK.getSound('happy').play();
} else if (self.stats.age >= 50 && self.evolution === 'child') {
self.evolution = 'adult';
self.updateEvolution();
LK.getSound('happy').play();
}
};
// Feed the Chao
self.feed = function (amount) {
self.stats.hunger = Math.min(100, self.stats.hunger + amount);
self.stats.energy = Math.min(100, self.stats.energy + amount / 2);
storage.chaoStats = self.stats;
updateStatsUI();
// Do happy animation
self.doHappyAnimation();
};
// Pet the Chao
self.pet = function () {
self.stats.happiness = Math.min(100, self.stats.happiness + 10);
storage.chaoStats = self.stats;
updateStatsUI();
LK.getSound('pet').play();
self.doHappyAnimation();
};
// Happy animation
self.doHappyAnimation = function () {
// Jump up and down
tween(self, {
y: self.y - 20
}, {
duration: 300,
easing: tween.easeOut,
onFinish: function onFinish() {
tween(self, {
y: self.y + 20
}, {
duration: 300,
easing: tween.easeIn
});
}
});
};
// Move Chao to random spot
self.moveToRandomSpot = function () {
// Set target to random position within garden bounds
self.targetX = 300 + Math.random() * 1400;
self.targetY = 600 + Math.random() * 500;
self.isWalking = true;
};
// Down event for petting
self.down = function (x, y, obj) {
self.pet();
};
// Update method called every frame
self.update = function () {
// Update stats
self.updateStats();
// Handle movement
if (self.isWalking) {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist < self.moveSpeed) {
// Reached target
self.x = self.targetX;
self.y = self.targetY;
self.isWalking = false;
} else {
// Move towards target
self.x += dx / dist * self.moveSpeed;
self.y += dy / dist * self.moveSpeed;
}
} else {
// Handle idle behavior
self.idleTime++;
if (self.idleTime > self.idleThreshold) {
self.idleTime = 0;
// Random chance to move
if (Math.random() < 0.3) {
self.moveToRandomSpot();
}
}
}
};
// Initialize evolution appearance
self.updateEvolution();
return self;
});
var ChaoEgg = Container.expand(function () {
var self = Container.call(this);
var eggGraphic = self.attachAsset('chaoEgg', {
anchorX: 0.5,
anchorY: 0.5
});
self.wobbling = false;
self.hatchProgress = 0;
self.hatchThreshold = 10;
self.wobble = function () {
if (self.wobbling) {
return;
}
self.wobbling = true;
tween(self, {
rotation: 0.1
}, {
duration: 300,
easing: tween.easeInOut,
onFinish: function onFinish() {
tween(self, {
rotation: -0.1
}, {
duration: 300,
easing: tween.easeInOut,
onFinish: function onFinish() {
self.wobbling = false;
self.rotation = 0;
}
});
}
});
self.hatchProgress++;
if (self.hatchProgress >= self.hatchThreshold) {
self.hatch();
}
};
self.hatch = function () {
LK.getSound('hatch').play();
// Create animation for hatching
tween(eggGraphic, {
alpha: 0
}, {
duration: 1000,
easing: tween.easeInOut,
onFinish: function onFinish() {
// Hatch complete, spawn Chao
createChao();
self.destroy();
}
});
};
self.down = function (x, y, obj) {
self.wobble();
};
return self;
});
var Fruit = Container.expand(function (type) {
var self = Container.call(this);
self.type = type || 'apple';
var color = 0xff5555;
if (self.type === 'orange') {
color = 0xff9933;
}
if (self.type === 'banana') {
color = 0xffee44;
}
var fruitGraphic = self.attachAsset('fruit' + self.type.charAt(0).toUpperCase() + self.type.slice(1), {
anchorX: 0.5,
anchorY: 0.5
});
self.nutritionValue = 10;
if (self.type === 'orange') {
self.nutritionValue = 15;
}
if (self.type === 'banana') {
self.nutritionValue = 20;
}
self.down = function (x, y, obj) {
self.dragging = true;
self.dragOffsetX = x - self.x;
self.dragOffsetY = y - self.y;
};
self.up = function (x, y, obj) {
self.dragging = false;
// Check if dropped on Chao
if (self.intersects(chao)) {
chao.feed(self.nutritionValue);
self.destroy();
fruitBasket.removeFruit(self);
LK.getSound('eat').play();
}
};
return self;
});
var FruitBasket = Container.expand(function () {
var self = Container.call(this);
self.fruits = [];
// Setup UI elements
var appleButton = self.attachAsset('fruitApple', {
anchorX: 0.5,
anchorY: 0.5,
x: 0
});
var orangeButton = self.attachAsset('fruitOrange', {
anchorX: 0.5,
anchorY: 0.5,
x: 60
});
var bananaButton = self.attachAsset('fruitBanana', {
anchorX: 0.5,
anchorY: 0.5,
x: 120
});
// Interaction handlers
appleButton.interactive = true;
orangeButton.interactive = true;
bananaButton.interactive = true;
appleButton.down = function () {
self.createFruit('apple');
};
orangeButton.down = function () {
self.createFruit('orange');
};
bananaButton.down = function () {
self.createFruit('banana');
};
// Create fruit
self.createFruit = function (type) {
var fruit = new Fruit(type);
fruit.x = self.x + self.fruits.length * 20 % 100;
fruit.y = self.y + 80;
self.fruits.push(fruit);
game.addChild(fruit);
};
// Remove fruit from array
self.removeFruit = function (fruit) {
var index = self.fruits.indexOf(fruit);
if (index !== -1) {
self.fruits.splice(index, 1);
}
};
return self;
});
var StatsBar = Container.expand(function (type) {
var self = Container.call(this);
self.type = type;
var color = 0x44cc44; // Green for hunger
if (self.type === 'happiness') {
color = 0x44aaff; // Blue for happiness
} else if (self.type === 'energy') {
color = 0xffcc44; // Yellow for energy
}
var background = self.attachAsset('statusBg', {
anchorX: 0,
anchorY: 0.5
});
var bar = self.attachAsset('statusBar', {
anchorX: 0,
anchorY: 0.5,
x: 10,
tint: color
});
var label = new Text2(self.type.charAt(0).toUpperCase() + self.type.slice(1), {
size: 24,
fill: 0x333333
});
label.anchor.set(0, 0.5);
label.x = 15;
self.addChild(label);
self.setValue = function (value) {
// Calculate width based on percentage
var maxWidth = 380;
var width = value / 100 * maxWidth;
tween(bar, {
width: width
}, {
duration: 300,
easing: tween.easeOut
});
};
return self;
});
var Toy = Container.expand(function () {
var self = Container.call(this);
var toyGraphic = self.attachAsset('toy', {
anchorX: 0.5,
anchorY: 0.5
});
self.down = function (x, y, obj) {
self.dragging = true;
self.dragOffsetX = x - self.x;
self.dragOffsetY = y - self.y;
};
self.up = function (x, y, obj) {
self.dragging = false;
// Check if dropped on Chao
if (self.intersects(chao)) {
chao.stats.happiness = Math.min(100, chao.stats.happiness + 15);
chao.stats.energy = Math.max(0, chao.stats.energy - 10);
storage.chaoStats = chao.stats;
updateStatsUI();
LK.getSound('happy').play();
chao.doHappyAnimation();
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x87CEEB
});
/****
* Game Code
****/
// UI Elements
var moneyText = new Text2('Money: $0', {
size: 36,
fill: 0xFFFFFF
});
moneyText.anchor.set(0, 0.5);
moneyText.x = 50;
moneyText.y = 500;
game.addChild(moneyText);
var garden = game.addChild(LK.getAsset('garden', {
anchorX: 0.5,
anchorY: 0,
x: 2048 / 2,
y: 400
}));
// Create pond
var pond = game.addChild(LK.getAsset('pond', {
anchorX: 0.5,
anchorY: 0.5,
x: 2048 / 2,
y: 800
}));
// UI Elements
var titleText = new Text2('Chao Paradise', {
size: 100,
fill: 0xFFFFFF
});
titleText.anchor.set(0.5, 0);
LK.gui.top.addChild(titleText);
// Stats UI
var hungerBar = new StatsBar('hunger');
hungerBar.x = 50;
hungerBar.y = 200;
game.addChild(hungerBar);
var happinessBar = new StatsBar('happiness');
happinessBar.x = 50;
happinessBar.y = 280;
game.addChild(happinessBar);
var energyBar = new StatsBar('energy');
energyBar.x = 50;
energyBar.y = 360;
game.addChild(energyBar);
var ageText = new Text2('Age: 0 days', {
size: 36,
fill: 0xFFFFFF
});
ageText.anchor.set(0, 0.5);
ageText.x = 50;
ageText.y = 440;
game.addChild(ageText);
// Update UI elements based on current stats
function updateStatsUI() {
if (!chao) {
return;
}
hungerBar.setValue(chao.stats.hunger);
happinessBar.setValue(chao.stats.happiness);
energyBar.setValue(chao.stats.energy);
ageText.setText('Age: ' + chao.stats.age + ' days');
// Game over if stats are too low for too long
if (chao.stats.hunger <= 0 && chao.stats.happiness <= 0 && chao.stats.energy <= 0) {
LK.showGameOver();
}
}
// Create food controls
var fruitBasket = new FruitBasket();
fruitBasket.x = 1600;
fruitBasket.y = 150;
game.addChild(fruitBasket);
// Create toy
var toy = new Toy();
toy.x = 1700;
toy.y = 250;
game.addChild(toy);
// Global variables
var chao = null;
var chaoEgg = null;
var dragTarget = null;
var dayCounter = 0;
var dayTimer = 0;
var dayLength = 1800; // 30 seconds at 60fps
// Start with an egg or existing Chao based on storage
function initializeChao() {
if (!storage.chaoStats || storage.chaoStats.age === 0) {
// New game, start with egg
chaoEgg = new ChaoEgg();
chaoEgg.x = 2048 / 2;
chaoEgg.y = 800;
game.addChild(chaoEgg);
} else {
// Existing Chao, restore it
createChao();
}
}
// Create a new Chao
function createChao() {
chao = new Chao();
chao.x = 2048 / 2;
chao.y = 800;
game.addChild(chao);
// Update UI with current stats
updateStatsUI();
}
// Drag handlers
game.down = function (x, y, obj) {
if (obj && obj.down) {
// Let the object handle its own down event
return;
}
};
game.move = function (x, y, obj) {
// Check all draggable items
if (toy && toy.dragging) {
toy.x = x - toy.dragOffsetX;
toy.y = y - toy.dragOffsetY;
}
// Check fruits
if (fruitBasket && fruitBasket.fruits) {
for (var i = 0; i < fruitBasket.fruits.length; i++) {
var fruit = fruitBasket.fruits[i];
if (fruit.dragging) {
fruit.x = x - fruit.dragOffsetX;
fruit.y = y - fruit.dragOffsetY;
}
}
}
};
game.up = function (x, y, obj) {
// Handled by individual objects
};
// Update game state each frame
game.update = function () {
// Handle day cycle
dayTimer++;
if (dayTimer >= dayLength) {
dayTimer = 0;
dayCounter++;
// Increment Chao age once per day
if (chao) {
chao.stats.age++;
storage.chaoStats = chao.stats;
updateStatsUI();
}
}
// Mini-game logic to earn money
if (dayCounter % 5 === 0) {
// Every 5 days, a mini-game is available
// Simulate mini-game result
var moneyEarned = Math.floor(Math.random() * 100); // Random money earned between 0 and 100
storage.money = (storage.money || 0) + moneyEarned;
moneyText.setText('Money: $' + storage.money);
}
};
// Initialize game
initializeChao();
LK.playMusic('gardenTheme'); ===================================================================
--- original.js
+++ change.js
@@ -411,9 +411,17 @@
/****
* Game Code
****/
-// Create garden background
+// UI Elements
+var moneyText = new Text2('Money: $0', {
+ size: 36,
+ fill: 0xFFFFFF
+});
+moneyText.anchor.set(0, 0.5);
+moneyText.x = 50;
+moneyText.y = 500;
+game.addChild(moneyText);
var garden = game.addChild(LK.getAsset('garden', {
anchorX: 0.5,
anchorY: 0,
x: 2048 / 2,
@@ -547,8 +555,16 @@
storage.chaoStats = chao.stats;
updateStatsUI();
}
}
+ // Mini-game logic to earn money
+ if (dayCounter % 5 === 0) {
+ // Every 5 days, a mini-game is available
+ // Simulate mini-game result
+ var moneyEarned = Math.floor(Math.random() * 100); // Random money earned between 0 and 100
+ storage.money = (storage.money || 0) + moneyEarned;
+ moneyText.setText('Money: $' + storage.money);
+ }
};
// Initialize game
initializeChao();
LK.playMusic('gardenTheme');
\ No newline at end of file