worldbox-minigame/entities.js

122 lines
2.8 KiB
JavaScript

/**********************************************************************
* ENTITY DEFINITIONS
**********************************************************************/
function createCitizen(name, x, y, forcedProfession=null) {
// If forcedProfession is provided, use it, otherwise random from PROFESSIONS
let profession = forcedProfession
? forcedProfession
: PROFESSIONS[Math.floor(Math.random() * PROFESSIONS.length)];
return {
name,
profession,
x, y,
vx: (Math.random() - 0.5) * 0.3,
vy: (Math.random() - 0.5) * 0.3,
color: `hsl(${Math.random() * 360}, 70%, 50%)`,
task: null,
target: null,
carryingWood: 0,
carryingFruit: 0,
carryingMedicine: 0,
carryingCapacity: 10,
hunger: 0,
energy: ENERGY_MAX,
health: HEALTH_MAX,
education: 0,
hasWeapon: profession === "Soldier" // Soldiers start with a weapon
};
}
function createResource(type, x, y, amount) {
return { type, x, y, amount };
}
function createHouseSite(x, y) {
return {
buildingType: "House",
x, y,
requiredWood: HOUSE_WOOD_REQUIRED,
deliveredWood: 0,
buildProgress: 0,
completed: false,
storedFruit: 0,
maxFruit: HOUSE_MAX_FRUIT
};
}
function createRoadSite(x1, y1, x2, y2) {
const mx = (x1 + x2) / 2;
const my = (y1 + y2) / 2;
return {
buildingType: "Road",
x: mx,
y: my,
x1, y1, x2, y2,
requiredWood: ROAD_WOOD_REQUIRED,
deliveredWood: 0,
buildProgress: 0,
completed: false
};
}
function createMarketSite(x, y) {
return {
buildingType: "Market",
x, y,
requiredWood: MARKET_WOOD_REQUIRED,
deliveredWood: 0,
buildProgress: 0,
completed: false,
lastIncomeTime: 0
};
}
function createHospitalSite(x, y) {
return {
buildingType: "Hospital",
x, y,
requiredWood: HOSPITAL_WOOD_REQUIRED,
deliveredWood: 0,
buildProgress: 0,
completed: false,
medicine: 0,
maxMedicine: 50
};
}
function createSchoolSite(x, y) {
return {
buildingType: "School",
x, y,
requiredWood: SCHOOL_WOOD_REQUIRED,
deliveredWood: 0,
buildProgress: 0,
completed: false,
knowledge: 0,
maxKnowledge: 100
};
}
function createAnimal(type, x, y) {
let cd = (type === "Rabbit") ? randInt(0, RABBIT_REPRO_COOLDOWN) : 0;
return {
type,
x, y,
vx: (Math.random() - 0.5) * 0.4,
vy: (Math.random() - 0.5) * 0.4,
hunger: 0,
dead: false,
reproductionCooldown: cd,
attackCooldown: 0
};
}
function spawnBabyAnimal(type, x, y) {
const nx = x + randInt(-20, 20);
const ny = y + randInt(-20, 20);
const baby = createAnimal(type, nx, ny);
animals.push(baby);
logAction(`A new baby ${type} is born!`);
}