/****
* Classes
****/
// Carnivore class
var Carnivore = Container.expand(function () {
var self = Container.call(this);
// Carnivore starvation logic
LK.on('tick', function () {
carnivores.forEach(function (carnivore, index) {
if (!carnivore.lastMealTime) {
carnivore.lastMealTime = LK.ticks;
}
if (LK.ticks - carnivore.lastMealTime > 300) {
// 5 seconds at 60FPS
carnivores.splice(index, 1);
carnivore.destroy();
}
});
});
// Update Carnivore eat method to reset lastMealTime on eating
Carnivore.prototype.eat = function () {
herbivores.forEach(function (herbivore, index) {
var dx = herbivore.x - self.x;
var dy = herbivore.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 50) {
herbivores.splice(index, 1);
herbivore.destroy();
self.speed += 0.1; // Evolve by increasing speed
self.lastMealTime = LK.ticks; // Reset last meal time to extend life for 6 seconds
// Spawn two new Carnivores at random locations
var newCarnivore1 = new Carnivore();
newCarnivore1.x = Math.random() * 2048;
newCarnivore1.y = Math.random() * 2732;
game.addChild(newCarnivore1);
carnivores.push(newCarnivore1);
var newCarnivore1 = new Carnivore();
newCarnivore1.x = Math.random() * 2048;
newCarnivore1.y = Math.random() * 2732;
game.addChild(newCarnivore1);
carnivores.push(newCarnivore1);
var newCarnivore2 = new Carnivore();
newCarnivore2.x = Math.random() * 2048;
newCarnivore2.y = Math.random() * 2732;
game.addChild(newCarnivore2);
carnivores.push(newCarnivore2);
var newCarnivore2 = new Carnivore();
newCarnivore2.x = Math.random() * 2048;
newCarnivore2.y = Math.random() * 2732;
game.addChild(newCarnivore2);
carnivores.push(newCarnivore2);
}
});
};
var carnivoreGraphics = self.attachAsset('carnivore', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 5; // Carnivores are significantly faster than herbivores
self.move = function () {
// Simple AI for movement towards nearest herbivore
var closestHerbivore = null;
var closestDistance = Infinity;
herbivores.forEach(function (herbivore) {
var dx = herbivore.x - self.x;
var dy = herbivore.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < closestDistance) {
closestHerbivore = herbivore;
closestDistance = distance;
}
});
if (closestHerbivore) {
var dx = closestHerbivore.x - self.x;
var dy = closestHerbivore.y - self.y;
var angle = Math.atan2(dy, dx);
self.x += Math.cos(angle) * self.speed;
self.y += Math.sin(angle) * self.speed;
}
};
self.eat = function () {
// Eat nearby herbivores
herbivores.forEach(function (herbivore, index) {
var dx = herbivore.x - self.x;
var dy = herbivore.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 50) {
herbivores.splice(index, 1);
herbivore.destroy();
self.speed += 0.1; // Evolve by increasing speed
self.lastMealTime = LK.ticks; // Reset last meal time to extend life for 6 seconds
}
});
};
});
// Food class
var Food = Container.expand(function () {
var self = Container.call(this);
var foodGraphics = self.attachAsset('food', {
anchorX: 0.5,
anchorY: 0.5
});
});
// Herbivore class
var Herbivore = Container.expand(function () {
var self = Container.call(this);
// Herbivore starvation logic
LK.on('tick', function () {
herbivores.forEach(function (herbivore, index) {
if (!herbivore.lastMealTime) {
herbivore.lastMealTime = LK.ticks;
}
if (LK.ticks - herbivore.lastMealTime > 360) {
// 6 seconds at 60FPS
herbivores.splice(index, 1);
herbivore.destroy();
}
});
});
var herbivoreGraphics = self.attachAsset('herbivore', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2; // Herbivores are further slowed to emphasize the speed difference with carnivores
self.reproductionRate = 0.01;
self.move = function () {
// Simple AI for movement towards nearest food
var closestFood = null;
var closestDistance = Infinity;
foods.forEach(function (food) {
var dx = food.x - self.x;
var dy = food.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < closestDistance) {
closestFood = food;
closestDistance = distance;
}
});
if (closestFood) {
var dx = closestFood.x - self.x;
var dy = closestFood.y - self.y;
var angle = Math.atan2(dy, dx);
self.x += Math.cos(angle) * self.speed;
self.y += Math.sin(angle) * self.speed;
}
};
self.eat = function () {
// Eat nearby food
foods.forEach(function (food, index) {
var dx = food.x - self.x;
var dy = food.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 50) {
foods.splice(index, 1);
food.destroy();
self.speed += 0.05; // Evolve by increasing speed
var newHerbivore = new Herbivore();
newHerbivore.x = Math.random() * 2048;
newHerbivore.y = Math.random() * 2732;
game.addChild(newHerbivore);
herbivores.push(newHerbivore);
var newHerbivore = new Herbivore();
newHerbivore.x = self.x;
newHerbivore.y = self.y;
game.addChild(newHerbivore);
herbivores.push(newHerbivore);
}
});
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
// Spawn food in the world periodically
// Initialize assets for carnivores, herbivores, and food.
LK.on('tick', function () {
if (LK.ticks % 100 == 0) {
// Every 1.67 seconds at 60FPS
var newFood = new Food();
newFood.x = Math.random() * 2048;
newFood.y = Math.random() * 2732;
game.addChild(newFood);
foods.push(newFood);
}
});
var carnivores = [];
var herbivores = [];
var foods = [];
// Populate the game with initial entities
function initializeEntities() {
for (var i = 0; i < 10; i++) {
var newCarnivore = new Carnivore();
newCarnivore.x = Math.random() * 2048;
newCarnivore.y = Math.random() * 2732;
game.addChild(newCarnivore);
carnivores.push(newCarnivore);
var newHerbivore = new Herbivore();
newHerbivore.x = Math.random() * 2048;
newHerbivore.y = Math.random() * 2732;
game.addChild(newHerbivore);
herbivores.push(newHerbivore);
}
for (var j = 0; j < 50; j++) {
var newFood = new Food();
newFood.x = Math.random() * 2048;
newFood.y = Math.random() * 2732;
game.addChild(newFood);
foods.push(newFood);
}
}
initializeEntities();
// Main game loop
LK.on('tick', function () {
// Move and eat logic for carnivores
carnivores.forEach(function (carnivore, index) {
carnivore.move();
// Example eat logic, actual implementation needed
if (Math.random() < 0.01) {
// Simulate eating
carnivore.eat();
}
});
// Move and eat logic for herbivores
herbivores.forEach(function (herbivore, index) {
herbivore.move();
// Example eat logic, actual implementation needed
if (Math.random() < 0.01) {
// Simulate eating
herbivore.eat();
}
});
// Food does not need to move, but could be consumed
});
// Note: This is a simplified structure. Actual movement, eating logic, and interactions between entities need to be implemented.