User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Fix Bug: 'TypeError: this.hasResources is not a function' in this line: 'if (utility.action === 'trade' && this.hasResources()) {' Line Number: 70
User prompt
Improve iq
User prompt
Fix Bug: 'Uncaught TypeError: self.createAndAddWorkplace is not a function' in this line: 'self.workplaces.push(self.createAndAddWorkplace());' Line Number: 455
User prompt
Fix Bug: 'Uncaught TypeError: self.createAndAddHouse is not a function' in this line: 'houses.push(self.createAndAddHouse(2048 / 2, 2732 / 2));' Line Number: 441
var Market = Container.expand(function () { var self = Container.call(this); var marketGraphics = self.createAsset('market', 'Market Graphics', .5, .5); self.x = 2048 / 2; self.y = 2732 / 2; self.availableResources = { wood: 0, rock: 0, food: 0 }; self.trade = function (villager, resourceType, amount) { if (self.availableResources[resourceType] >= amount) { self.availableResources[resourceType] -= amount; villager.gameResources[resourceType].amount += amount; } }; self.addResource = function (resourceType, amount) { self.availableResources[resourceType] += amount; }; }); var HouseBuilder = Container.expand(function () { var self = Container.call(this); self.build = function (villager) { var newHouse = new House(); newHouse.initializeRandomPosition(); villager.parent.addChild(newHouse); villager.state = 'atHome'; villager.home = newHouse; newHouse.scheduleVillagerWork(villager); }; }); var Resource = Container.expand(function (type, amount) { var self = Container.call(this); self.type = type; self.amount = amount; var resourceGraphics = self.createAsset(type.toLowerCase() + 'Image', type + ' Image', .5, .5); }); var Tree = Container.expand(function () { var self = Container.call(this); var treeGraphics = self.createAsset('treeImage', 'Tree Image', .5, .5); self.resourceType = 'food'; self.amount = 100; self.collectResource = function (villager) { if (self.amount > 0) { var collectedAmount = Math.min(5, self.amount); villager.gameResources.food.amount += collectedAmount; villager.carriedFood += collectedAmount; self.amount -= collectedAmount; if (self.amount <= 0) { self.destroy(); } } }; }); var Villager = Container.expand(function (resources) { var self = Container.call(this); Villager.prototype.kiss = function (partner) { if (this.canFallInLove(partner)) { this.createBabyWith(partner); } }; Villager.prototype.createBabyWith = function (partner) { var babyX = (this.x + partner.x) / 2; var babyY = (this.y + partner.y) / 2; var babyVillager = this.parent.createAndAddVillager(babyX, babyY, true); babyVillager.home = this.home || partner.home; babyVillager.workplace = this.workplace || partner.workplace; }; Villager.prototype.continuousMove = function () { if (this.x < this.speed || this.x > 2048 - this.speed) this.directionX *= -1; if (this.y < this.speed || this.y > 2732 - this.speed) this.directionY *= -1; this.x = Math.max(this.speed, Math.min(2048 - this.speed, this.x + this.speed * this.directionX)); this.y = Math.max(this.speed, Math.min(2732 - this.speed, this.y + this.speed * this.directionY)); if (Math.random() < 0.5) { this.directionX = Math.random() < 0.5 ? -1 : 1; this.directionY = Math.random() < 0.5 ? -1 : 1; } }; Villager.prototype.directionX = 1; Villager.prototype.directionY = 1; Villager.prototype.restAtHome = function () { this.state = 'resting'; this.tiredness = Math.max(0, this.tiredness - 0.1); if (this.home) { this.home.occupy(this); } if (this.tiredness <= 0) { this.state = 'atHome'; } }; Villager.prototype.isDoingSomething = function () { return this.state === 'goingToWork' || this.state === 'working' || this.state === 'goingHome' || this.state === 'resting' || this.state === 'plantingTree' || this.state === 'buildingHouse'; }; Villager.prototype.carriedWood = 0; Villager.prototype.carriedRock = 0; Villager.prototype.carriedFood = 0; Villager.prototype.tiredness = 0; Villager.prototype.canBuildHouse = function () { return this.gameResources && this.gameResources.wood.amount >= 200 && this.gameResources.rock.amount >= 200 && this.gameResources.food.amount >= 200; }; Villager.prototype.calculateBuildUtility = function () { return this.canBuildHouse() ? 25000 : 0; }; Villager.prototype.hasResources = function () { return this.carriedWood > 0 || this.carriedRock > 0 || this.carriedFood > 0; }; Villager.prototype.canFallInLove = function (otherVillager) { return this.state === 'atHome' && otherVillager.state === 'atHome' && !this.partner && !otherVillager.partner; }; self.gameResources = resources; var villagerGraphics = self.createAsset('villager', 'Villager Graphics', .5, .5); self.targetX = null; self.targetY = null; self.home = null; self.workplace = null; self.resourceType = null; self.speed = 12; self.moveTo = function (x, y, state) { self.targetX = x; self.targetY = y; if (state) self.state = state; }; self.update = function () { self.continuousMove(); self.executeCurrentAction(); }; self.decideNextAction = function () { var utilities = [{ action: 'work', utility: self.calculateWorkUtility() }, { action: 'home', utility: self.calculateHomeUtility() }, { action: 'plant', utility: self.calculatePlantUtility(self.gameResources) }, { action: 'buildHouse', utility: self.calculateBuildUtility() }, { action: 'trade', utility: self.calculateTradeUtility() }]; var highestUtilityAction = utilities.reduce(function (highest, current) { return highest.utility > current.utility ? highest : current; }); self.state = highestUtilityAction.action; if (self.state === 'work') { self.moveTo(self.workplace.x, self.workplace.y, 'goingToWork'); } }; self.executeCurrentAction = function () { switch (self.state) { case 'goingToWork': if (self.workplace && self.workplace.x !== null && self.workplace.y !== null) { self.moveTowards(self.workplace.x, self.workplace.y); if (self.intersects(self.workplace)) { self.state = 'working'; self.startWorking(); } } break; case 'working': if (self.workplace && typeof self.workplace.collectResource === 'function') { self.workplace.collectResource(self); } break; case 'goingHome': if (self.home) { self.moveTowards(self.home.x, self.home.y); if (self.intersects(self.home)) { self.arriveHome(); } } break; case 'resting': self.restAtHome(); break; case 'plantingTree': self.plantTree(); break; case 'buildingHouse': self.buildHouse(); break; case 'trade': if (self.parent.market) { self.moveTowards(self.parent.market.x, self.parent.market.y); if (self.intersects(self.parent.market)) { if (self.carriedWood > 10) self.parent.market.trade(self, 'wood', 10); if (self.carriedRock > 10) self.parent.market.trade(self, 'rock', 10); if (self.carriedFood > 10) self.parent.market.trade(self, 'food', 10); self.state = 'atHome'; } } break; } }; self.calculateWorkUtility = function () { return self.workplace && self.workplace.resourceType && self.workplace.resourceType.amount > 0 ? 25000 : 1; }; self.calculateHomeUtility = function () { return self.tiredness > 0.7 || self.hasResources() ? 25000 : 1; }; self.calculatePlantUtility = function (gameResources) { return self.gameResources && self.gameResources.food.amount > 50 && !self.workplace ? 25000 : 0; }; self.calculateTradeUtility = function () { var excessResources = (self.carriedWood > 10 ? 1 : 0) + (self.carriedRock > 10 ? 1 : 0) + (self.carriedFood > 10 ? 1 : 0); return excessResources > 1 ? 25000 : 0; }; self.moveTowards = function (x, y) { var dx = x - self.x; var dy = y - self.y; if (dx !== 0 || dy !== 0) { self.moveTo(x, y); self.moveTowardsTarget(); } }; self.moveTowardsTarget = function () { if (self.targetX !== null && self.targetY !== null) { var dx = self.targetX - self.x; var dy = self.targetY - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < self.speed) { self.x = self.targetX; self.y = self.targetY; self.targetX = null; self.targetY = null; } else { var angle = Math.atan2(dy, dx); self.x += Math.cos(angle) * Math.min(self.speed, distance); self.y += Math.sin(angle) * Math.min(self.speed, distance); } } }; }); var EmployingWorkPlace = Container.expand(function () { var self = Container.call(this); var workplaceGraphics = self.createAsset('workplace', 'Workplace Graphics', .5, .5); self.isEmploying = true; self.employ = function (villager) {}; }); var House = Container.expand(function () { var self = Container.call(this); House.prototype.scheduleVillagerWork = function (villager) { LK.setTimeout(function () { villager.state = 'goingToWork'; villager.moveTo(villager.workplace.x, villager.workplace.y); }, 5000); }; House.prototype.initializeRandomPosition = function () { this.x = Math.random() * 2048; this.y = Math.random() * 2732; }; var houseGraphics = self.createAsset('house', 'House Graphics', .5, .5); self.isOccupiable = true; House.prototype.occupy = function (villager) { if (!this.isOccupiable) return; this.isOccupiable = false; villager.home = this; this.scheduleVillagerWork(villager); }; }); var ResourceDisplay = Container.expand(function (gameResources) { var self = Container.call(this); self.gameResources = gameResources || ({ wood: { amount: 0 }, rock: { amount: 0 }, food: { amount: 0 } }); var resourceDisplayText = new Text2('', { size: 50, fill: "#ffffff" }); resourceDisplayText.anchor.set(0.5, 0); resourceDisplayText.x = 2048 / 2 - 800 - 500 + 100; resourceDisplayText.y = 50; LK.gui.topCenter.addChild(resourceDisplayText); self.update = function () { resourceDisplayText.setText('Resources: Wood ' + self.gameResources.wood.amount + ' units, Rock ' + self.gameResources.rock.amount + ' units, Food ' + self.gameResources.food.amount + ' units'); }; }); var VillagerAndFramerateDisplay = Container.expand(function (game) { var self = Container.call(this); self.villagers = game.villagers; var villagerCountText = new Text2('', { size: 50, fill: "#ffffff" }); villagerCountText.anchor.set(0.5, 0); villagerCountText.x = 2048 / 2 - 200; villagerCountText.y = 50; LK.gui.topCenter.addChild(villagerCountText); var startTime = Date.now(); var framerateText = new Text2('', { size: 50, fill: "#ffffff" }); framerateText.anchor.set(0, 1); framerateText.x = 500; framerateText.y = 2732; LK.gui.topCenter.addChild(framerateText); var framerateText = new Text2('', { size: 50, fill: "#ffffff" }); framerateText.anchor.set(0, 1); framerateText.x = 500; framerateText.y = 2732; LK.gui.topCenter.addChild(framerateText); var lastTick = LK.ticks; var frameCount = 0; var lastSecond = Math.floor(lastTick / 60); self.update = function () { var currentTick = LK.ticks; var currentSecond = Math.floor(currentTick / 60); frameCount++; if (currentSecond > lastSecond) { framerateText.setText('FPS: ' + frameCount); framerateText.visible = true; frameCount = 0; lastSecond = currentSecond; } villagerCountText.setText('Villagers: ' + (self.villagers ? self.villagers.length : 0)); }; }); var Game = Container.expand(function () { var self = Container.call(this); self.makeVillagerDecisions = function (villager) { if (!villager.isDoingSomething()) { villager.decideNextAction(); } }; LK.on('tick', function () { self.villagerAndFramerateDisplay.update(); var currentTime = Date.now(); var elapsedTime = Math.floor((currentTime - self.startTime) / 1000); self.timerText.setText('Time: ' + elapsedTime); self.updateResourceDisplay(); for (var i = 0; i < self.villagers.length; i++) { self.villagers[i].update(); self.makeVillagerDecisions(self.villagers[i]); } self.handleVillagersFallingInLove(); }); self.timerText = new Text2('Time: 0', { size: 100, fill: "#ffffff" }); self.timerText.anchor.set(1, 1); self.timerText.x = 2048 - 500; self.timerText.y = 2732; LK.gui.topCenter.addChild(self.timerText); self.startTime = Date.now(); self.villagers = []; self.villagerAndFramerateDisplay = new VillagerAndFramerateDisplay(self); self.resourceDisplay = new ResourceDisplay(resources); self.updateResourceDisplay = function () { self.resourceDisplay.update(); }; self.handleVillagersFallingInLove = function () { for (var i = 0; i < self.villagers.length; i++) { for (var j = i + 1; j < self.villagers.length; j++) { var villagerA = self.villagers[i]; var villagerB = self.villagers[j]; if (villagerA.canFallInLove(villagerB) && Math.random() < 0.5) { villagerA.partner = villagerB; villagerB.partner = villagerA; LK.setTimeout(function () { var home = villagerA.home || villagerB.home; var childX = home.x; var childY = home.y; var childVillager = self.createAndAddVillager(childX, childY, true); childVillager.home = home; childVillager.workplace = self.workplaces[Math.floor(Math.random() * self.workplaces.length)]; childVillager.state = 'goingToWork'; childVillager.moveTo(childVillager.workplace.x, childVillager.workplace.y); villagerA.kiss(villagerB); }, 1000); break; } } } }; function createResources() { return { wood: new Resource('Wood', 100), rock: new Resource('Rock', 100), food: new Resource('Food', 100) }; } var resources = createResources(); self.addChild(resources.wood); self.addChild(resources.rock); self.addChild(resources.food); var background = self.createAsset('background', 'Background Graphics', 0, 0); background.width = 4096 * 0.8; background.height = 5464 * 0.8; background.x = (2048 - background.width) / 2; background.y = (2732 - background.height) / 2 - 400; self.addChildAt(background, 0); Game.prototype.createVillager = function (x, y, isBaby) { var villager = new Villager(this.resources); villager.x = x; villager.y = y; if (isBaby) { villager.scale.x = 0.5; villager.scale.y = 0.5; } return villager; }; Game.prototype.addVillager = function (villager) { this.addChild(villager); this.villagers.push(villager); }; self.villagers = []; self.houses = []; var newVillager = self.createVillager(1024, 1366); self.addVillager(newVillager); var newVillager = self.createVillager(1024, 1500); self.addVillager(newVillager); self.market = new Market(); self.addChild(self.market); Game.prototype.createHouse = function (x, y) { var house = new House(); house.x = x; house.y = y; return house; }; Game.prototype.addHouse = function (house) { this.addChild(house); this.houses.push(house); }; var houses = []; var newHouse = self.createHouse(2048 / 2, 2732 / 2); self.addHouse(newHouse); houses.push(newHouse); Game.prototype.createWorkplace = function () { var workplace = new EmployingWorkPlace(); workplace.x = Math.random() * 2048; workplace.y = Math.random() * 2732; return workplace; }; Game.prototype.addWorkplace = function (workplace) { this.addChild(workplace); this.workplaces.push(workplace); }; self.workplaces = []; self.workplaces.push(self.createAndAddWorkplace()); var house = new House(); house.x = 2048 / 2; house.y = 2732 / 2; houses.push(house); self.addChild(house); var workplace = new EmployingWorkPlace(); workplace.x = Math.random() * 2048; workplace.y = Math.random() * 2732; self.workplaces.push(workplace); self.addChild(workplace); self.villagerCreationInterval = LK.setInterval(function () { if (self.resources && self.resources.food.amount >= 10) { self.createAndAddVillager(Math.random() * 2048, Math.random() * 2732); self.resources.food.amount -= 10; } }, 10000); LK.on('tick', function () { self.villagerAndFramerateDisplay.update(); var currentTime = Date.now(); var elapsedTime = Math.floor((currentTime - self.startTime) / 1000); self.timerText.setText('Time: ' + elapsedTime); self.updateResourceDisplay(); for (var i = 0; i < self.villagers.length; i++) { self.villagers[i].update(); } self.handleVillagersFallingInLove(); }); });
var Market = Container.expand(function () {
var self = Container.call(this);
var marketGraphics = self.createAsset('market', 'Market Graphics', .5, .5);
self.x = 2048 / 2;
self.y = 2732 / 2;
self.availableResources = {
wood: 0,
rock: 0,
food: 0
};
self.trade = function (villager, resourceType, amount) {
if (self.availableResources[resourceType] >= amount) {
self.availableResources[resourceType] -= amount;
villager.gameResources[resourceType].amount += amount;
}
};
self.addResource = function (resourceType, amount) {
self.availableResources[resourceType] += amount;
};
});
var HouseBuilder = Container.expand(function () {
var self = Container.call(this);
self.build = function (villager) {
var newHouse = new House();
newHouse.initializeRandomPosition();
villager.parent.addChild(newHouse);
villager.state = 'atHome';
villager.home = newHouse;
newHouse.scheduleVillagerWork(villager);
};
});
var Resource = Container.expand(function (type, amount) {
var self = Container.call(this);
self.type = type;
self.amount = amount;
var resourceGraphics = self.createAsset(type.toLowerCase() + 'Image', type + ' Image', .5, .5);
});
var Tree = Container.expand(function () {
var self = Container.call(this);
var treeGraphics = self.createAsset('treeImage', 'Tree Image', .5, .5);
self.resourceType = 'food';
self.amount = 100;
self.collectResource = function (villager) {
if (self.amount > 0) {
var collectedAmount = Math.min(5, self.amount);
villager.gameResources.food.amount += collectedAmount;
villager.carriedFood += collectedAmount;
self.amount -= collectedAmount;
if (self.amount <= 0) {
self.destroy();
}
}
};
});
var Villager = Container.expand(function (resources) {
var self = Container.call(this);
Villager.prototype.kiss = function (partner) {
if (this.canFallInLove(partner)) {
this.createBabyWith(partner);
}
};
Villager.prototype.createBabyWith = function (partner) {
var babyX = (this.x + partner.x) / 2;
var babyY = (this.y + partner.y) / 2;
var babyVillager = this.parent.createAndAddVillager(babyX, babyY, true);
babyVillager.home = this.home || partner.home;
babyVillager.workplace = this.workplace || partner.workplace;
};
Villager.prototype.continuousMove = function () {
if (this.x < this.speed || this.x > 2048 - this.speed) this.directionX *= -1;
if (this.y < this.speed || this.y > 2732 - this.speed) this.directionY *= -1;
this.x = Math.max(this.speed, Math.min(2048 - this.speed, this.x + this.speed * this.directionX));
this.y = Math.max(this.speed, Math.min(2732 - this.speed, this.y + this.speed * this.directionY));
if (Math.random() < 0.5) {
this.directionX = Math.random() < 0.5 ? -1 : 1;
this.directionY = Math.random() < 0.5 ? -1 : 1;
}
};
Villager.prototype.directionX = 1;
Villager.prototype.directionY = 1;
Villager.prototype.restAtHome = function () {
this.state = 'resting';
this.tiredness = Math.max(0, this.tiredness - 0.1);
if (this.home) {
this.home.occupy(this);
}
if (this.tiredness <= 0) {
this.state = 'atHome';
}
};
Villager.prototype.isDoingSomething = function () {
return this.state === 'goingToWork' || this.state === 'working' || this.state === 'goingHome' || this.state === 'resting' || this.state === 'plantingTree' || this.state === 'buildingHouse';
};
Villager.prototype.carriedWood = 0;
Villager.prototype.carriedRock = 0;
Villager.prototype.carriedFood = 0;
Villager.prototype.tiredness = 0;
Villager.prototype.canBuildHouse = function () {
return this.gameResources && this.gameResources.wood.amount >= 200 && this.gameResources.rock.amount >= 200 && this.gameResources.food.amount >= 200;
};
Villager.prototype.calculateBuildUtility = function () {
return this.canBuildHouse() ? 25000 : 0;
};
Villager.prototype.hasResources = function () {
return this.carriedWood > 0 || this.carriedRock > 0 || this.carriedFood > 0;
};
Villager.prototype.canFallInLove = function (otherVillager) {
return this.state === 'atHome' && otherVillager.state === 'atHome' && !this.partner && !otherVillager.partner;
};
self.gameResources = resources;
var villagerGraphics = self.createAsset('villager', 'Villager Graphics', .5, .5);
self.targetX = null;
self.targetY = null;
self.home = null;
self.workplace = null;
self.resourceType = null;
self.speed = 12;
self.moveTo = function (x, y, state) {
self.targetX = x;
self.targetY = y;
if (state) self.state = state;
};
self.update = function () {
self.continuousMove();
self.executeCurrentAction();
};
self.decideNextAction = function () {
var utilities = [{
action: 'work',
utility: self.calculateWorkUtility()
}, {
action: 'home',
utility: self.calculateHomeUtility()
}, {
action: 'plant',
utility: self.calculatePlantUtility(self.gameResources)
}, {
action: 'buildHouse',
utility: self.calculateBuildUtility()
}, {
action: 'trade',
utility: self.calculateTradeUtility()
}];
var highestUtilityAction = utilities.reduce(function (highest, current) {
return highest.utility > current.utility ? highest : current;
});
self.state = highestUtilityAction.action;
if (self.state === 'work') {
self.moveTo(self.workplace.x, self.workplace.y, 'goingToWork');
}
};
self.executeCurrentAction = function () {
switch (self.state) {
case 'goingToWork':
if (self.workplace && self.workplace.x !== null && self.workplace.y !== null) {
self.moveTowards(self.workplace.x, self.workplace.y);
if (self.intersects(self.workplace)) {
self.state = 'working';
self.startWorking();
}
}
break;
case 'working':
if (self.workplace && typeof self.workplace.collectResource === 'function') {
self.workplace.collectResource(self);
}
break;
case 'goingHome':
if (self.home) {
self.moveTowards(self.home.x, self.home.y);
if (self.intersects(self.home)) {
self.arriveHome();
}
}
break;
case 'resting':
self.restAtHome();
break;
case 'plantingTree':
self.plantTree();
break;
case 'buildingHouse':
self.buildHouse();
break;
case 'trade':
if (self.parent.market) {
self.moveTowards(self.parent.market.x, self.parent.market.y);
if (self.intersects(self.parent.market)) {
if (self.carriedWood > 10) self.parent.market.trade(self, 'wood', 10);
if (self.carriedRock > 10) self.parent.market.trade(self, 'rock', 10);
if (self.carriedFood > 10) self.parent.market.trade(self, 'food', 10);
self.state = 'atHome';
}
}
break;
}
};
self.calculateWorkUtility = function () {
return self.workplace && self.workplace.resourceType && self.workplace.resourceType.amount > 0 ? 25000 : 1;
};
self.calculateHomeUtility = function () {
return self.tiredness > 0.7 || self.hasResources() ? 25000 : 1;
};
self.calculatePlantUtility = function (gameResources) {
return self.gameResources && self.gameResources.food.amount > 50 && !self.workplace ? 25000 : 0;
};
self.calculateTradeUtility = function () {
var excessResources = (self.carriedWood > 10 ? 1 : 0) + (self.carriedRock > 10 ? 1 : 0) + (self.carriedFood > 10 ? 1 : 0);
return excessResources > 1 ? 25000 : 0;
};
self.moveTowards = function (x, y) {
var dx = x - self.x;
var dy = y - self.y;
if (dx !== 0 || dy !== 0) {
self.moveTo(x, y);
self.moveTowardsTarget();
}
};
self.moveTowardsTarget = function () {
if (self.targetX !== null && self.targetY !== null) {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.speed) {
self.x = self.targetX;
self.y = self.targetY;
self.targetX = null;
self.targetY = null;
} else {
var angle = Math.atan2(dy, dx);
self.x += Math.cos(angle) * Math.min(self.speed, distance);
self.y += Math.sin(angle) * Math.min(self.speed, distance);
}
}
};
});
var EmployingWorkPlace = Container.expand(function () {
var self = Container.call(this);
var workplaceGraphics = self.createAsset('workplace', 'Workplace Graphics', .5, .5);
self.isEmploying = true;
self.employ = function (villager) {};
});
var House = Container.expand(function () {
var self = Container.call(this);
House.prototype.scheduleVillagerWork = function (villager) {
LK.setTimeout(function () {
villager.state = 'goingToWork';
villager.moveTo(villager.workplace.x, villager.workplace.y);
}, 5000);
};
House.prototype.initializeRandomPosition = function () {
this.x = Math.random() * 2048;
this.y = Math.random() * 2732;
};
var houseGraphics = self.createAsset('house', 'House Graphics', .5, .5);
self.isOccupiable = true;
House.prototype.occupy = function (villager) {
if (!this.isOccupiable) return;
this.isOccupiable = false;
villager.home = this;
this.scheduleVillagerWork(villager);
};
});
var ResourceDisplay = Container.expand(function (gameResources) {
var self = Container.call(this);
self.gameResources = gameResources || ({
wood: {
amount: 0
},
rock: {
amount: 0
},
food: {
amount: 0
}
});
var resourceDisplayText = new Text2('', {
size: 50,
fill: "#ffffff"
});
resourceDisplayText.anchor.set(0.5, 0);
resourceDisplayText.x = 2048 / 2 - 800 - 500 + 100;
resourceDisplayText.y = 50;
LK.gui.topCenter.addChild(resourceDisplayText);
self.update = function () {
resourceDisplayText.setText('Resources: Wood ' + self.gameResources.wood.amount + ' units, Rock ' + self.gameResources.rock.amount + ' units, Food ' + self.gameResources.food.amount + ' units');
};
});
var VillagerAndFramerateDisplay = Container.expand(function (game) {
var self = Container.call(this);
self.villagers = game.villagers;
var villagerCountText = new Text2('', {
size: 50,
fill: "#ffffff"
});
villagerCountText.anchor.set(0.5, 0);
villagerCountText.x = 2048 / 2 - 200;
villagerCountText.y = 50;
LK.gui.topCenter.addChild(villagerCountText);
var startTime = Date.now();
var framerateText = new Text2('', {
size: 50,
fill: "#ffffff"
});
framerateText.anchor.set(0, 1);
framerateText.x = 500;
framerateText.y = 2732;
LK.gui.topCenter.addChild(framerateText);
var framerateText = new Text2('', {
size: 50,
fill: "#ffffff"
});
framerateText.anchor.set(0, 1);
framerateText.x = 500;
framerateText.y = 2732;
LK.gui.topCenter.addChild(framerateText);
var lastTick = LK.ticks;
var frameCount = 0;
var lastSecond = Math.floor(lastTick / 60);
self.update = function () {
var currentTick = LK.ticks;
var currentSecond = Math.floor(currentTick / 60);
frameCount++;
if (currentSecond > lastSecond) {
framerateText.setText('FPS: ' + frameCount);
framerateText.visible = true;
frameCount = 0;
lastSecond = currentSecond;
}
villagerCountText.setText('Villagers: ' + (self.villagers ? self.villagers.length : 0));
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
self.makeVillagerDecisions = function (villager) {
if (!villager.isDoingSomething()) {
villager.decideNextAction();
}
};
LK.on('tick', function () {
self.villagerAndFramerateDisplay.update();
var currentTime = Date.now();
var elapsedTime = Math.floor((currentTime - self.startTime) / 1000);
self.timerText.setText('Time: ' + elapsedTime);
self.updateResourceDisplay();
for (var i = 0; i < self.villagers.length; i++) {
self.villagers[i].update();
self.makeVillagerDecisions(self.villagers[i]);
}
self.handleVillagersFallingInLove();
});
self.timerText = new Text2('Time: 0', {
size: 100,
fill: "#ffffff"
});
self.timerText.anchor.set(1, 1);
self.timerText.x = 2048 - 500;
self.timerText.y = 2732;
LK.gui.topCenter.addChild(self.timerText);
self.startTime = Date.now();
self.villagers = [];
self.villagerAndFramerateDisplay = new VillagerAndFramerateDisplay(self);
self.resourceDisplay = new ResourceDisplay(resources);
self.updateResourceDisplay = function () {
self.resourceDisplay.update();
};
self.handleVillagersFallingInLove = function () {
for (var i = 0; i < self.villagers.length; i++) {
for (var j = i + 1; j < self.villagers.length; j++) {
var villagerA = self.villagers[i];
var villagerB = self.villagers[j];
if (villagerA.canFallInLove(villagerB) && Math.random() < 0.5) {
villagerA.partner = villagerB;
villagerB.partner = villagerA;
LK.setTimeout(function () {
var home = villagerA.home || villagerB.home;
var childX = home.x;
var childY = home.y;
var childVillager = self.createAndAddVillager(childX, childY, true);
childVillager.home = home;
childVillager.workplace = self.workplaces[Math.floor(Math.random() * self.workplaces.length)];
childVillager.state = 'goingToWork';
childVillager.moveTo(childVillager.workplace.x, childVillager.workplace.y);
villagerA.kiss(villagerB);
}, 1000);
break;
}
}
}
};
function createResources() {
return {
wood: new Resource('Wood', 100),
rock: new Resource('Rock', 100),
food: new Resource('Food', 100)
};
}
var resources = createResources();
self.addChild(resources.wood);
self.addChild(resources.rock);
self.addChild(resources.food);
var background = self.createAsset('background', 'Background Graphics', 0, 0);
background.width = 4096 * 0.8;
background.height = 5464 * 0.8;
background.x = (2048 - background.width) / 2;
background.y = (2732 - background.height) / 2 - 400;
self.addChildAt(background, 0);
Game.prototype.createVillager = function (x, y, isBaby) {
var villager = new Villager(this.resources);
villager.x = x;
villager.y = y;
if (isBaby) {
villager.scale.x = 0.5;
villager.scale.y = 0.5;
}
return villager;
};
Game.prototype.addVillager = function (villager) {
this.addChild(villager);
this.villagers.push(villager);
};
self.villagers = [];
self.houses = [];
var newVillager = self.createVillager(1024, 1366);
self.addVillager(newVillager);
var newVillager = self.createVillager(1024, 1500);
self.addVillager(newVillager);
self.market = new Market();
self.addChild(self.market);
Game.prototype.createHouse = function (x, y) {
var house = new House();
house.x = x;
house.y = y;
return house;
};
Game.prototype.addHouse = function (house) {
this.addChild(house);
this.houses.push(house);
};
var houses = [];
var newHouse = self.createHouse(2048 / 2, 2732 / 2);
self.addHouse(newHouse);
houses.push(newHouse);
Game.prototype.createWorkplace = function () {
var workplace = new EmployingWorkPlace();
workplace.x = Math.random() * 2048;
workplace.y = Math.random() * 2732;
return workplace;
};
Game.prototype.addWorkplace = function (workplace) {
this.addChild(workplace);
this.workplaces.push(workplace);
};
self.workplaces = [];
self.workplaces.push(self.createAndAddWorkplace());
var house = new House();
house.x = 2048 / 2;
house.y = 2732 / 2;
houses.push(house);
self.addChild(house);
var workplace = new EmployingWorkPlace();
workplace.x = Math.random() * 2048;
workplace.y = Math.random() * 2732;
self.workplaces.push(workplace);
self.addChild(workplace);
self.villagerCreationInterval = LK.setInterval(function () {
if (self.resources && self.resources.food.amount >= 10) {
self.createAndAddVillager(Math.random() * 2048, Math.random() * 2732);
self.resources.food.amount -= 10;
}
}, 10000);
LK.on('tick', function () {
self.villagerAndFramerateDisplay.update();
var currentTime = Date.now();
var elapsedTime = Math.floor((currentTime - self.startTime) / 1000);
self.timerText.setText('Time: ' + elapsedTime);
self.updateResourceDisplay();
for (var i = 0; i < self.villagers.length; i++) {
self.villagers[i].update();
}
self.handleVillagersFallingInLove();
});
});
A background of a paysage with plain and without tree or montainbut no water see from the top in the air 100m by 100m Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
One human medieval villager in a warcraft 2 style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a house in a warcraft 2 style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A tree a rock and crambery in a RTS style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A heart comic style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A cloud in a comic style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A market in a RTS fantasy style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
An Inn in a RTS fantasy style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
An school in a RTS fantasy style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
An bakery in a RTS fantasy style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A well in a RTS fantasy style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A tree in a rts style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
An apple in a rts style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A rock in a rts style Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A home in a RTS and fantasy style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A brewery in a fantasy rts style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A lumberjack in a fantasy rts style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a marketplace in a fantasy rts style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a festival in a fantasy rts style. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.