Spaces:
Running
Running
File size: 6,879 Bytes
a32dc8b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
// No direct THREE.js import needed
/**
* Daily Routine System for Villager AI
*
* This implementation manages villager daily routines including work, rest, and social interactions.
* It uses a schedule-based approach with some randomness to create natural-looking behavior.
*/
class DailyRoutineManager {
constructor() {
this.schedules = new Map(); // Map of villager IDs to schedules
this.currentTime = 0; // In-game time in hours (0-24)
}
/**
* Set a schedule for a villager
* @param {string} villagerId - Villager identifier
* @param {Array} schedule - Array of schedule entries
*/
setSchedule(villagerId, schedule) {
this.schedules.set(villagerId, schedule);
}
/**
* Update the routine manager
* @param {number} deltaTime - Time since last update in hours
*/
update(deltaTime) {
this.currentTime += deltaTime;
if (this.currentTime >= 24) {
this.currentTime -= 24; // Wrap around to next day
}
}
/**
* Get current activity for a villager
* @param {string} villagerId - Villager identifier
* @returns {Object} Current activity
*/
getCurrentActivity(villagerId) {
const schedule = this.schedules.get(villagerId);
if (!schedule) return null;
// Find the current activity based on time
for (let i = schedule.length - 1; i >= 0; i--) {
if (this.currentTime >= schedule[i].startTime) {
return schedule[i];
}
}
// If no activity found, return the last one (for times before first activity)
return schedule[schedule.length - 1];
}
/**
* Create a default schedule for a villager
* @param {string} villagerId - Villager identifier
* @param {Object} workLocation - Work location
* @param {Object} homeLocation - Home location
* @param {Array} socialLocations - Social locations
*/
createDefaultSchedule(villagerId, workLocation, homeLocation, socialLocations) {
const schedule = [
{ startTime: 0, activity: 'sleep', location: homeLocation },
{ startTime: 7, activity: 'eat', location: homeLocation },
{ startTime: 8, activity: 'work', location: workLocation },
{ startTime: 12, activity: 'eat', location: workLocation },
{ startTime: 13, activity: 'work', location: workLocation },
{ startTime: 17, activity: 'socialize', location: socialLocations[0] },
{ startTime: 19, activity: 'eat', location: homeLocation },
{ startTime: 20, activity: 'sleep', location: homeLocation }
];
this.setSchedule(villagerId, schedule);
}
}
/**
* Villager class with routine-based behavior
*/
class Villager {
constructor(id, position) {
this.id = id;
this.position = position;
this.velocity = [0, 0, 0]; // Simple array instead of THREE.Vector3
this.maxSpeed = 2;
this.energy = 100;
this.hunger = 0;
this.socialNeed = 0;
this.state = 'idle'; // idle, working, sleeping, eating, socializing
this.path = [];
this.avoidanceRadius = 2;
this.avoidanceWeight = 1.5;
this.steeringWeight = 1.0;
}
/**
* Calculate distance between two points
* @param {Array} pos1 - First position [x, y, z]
* @param {Array} pos2 - Second position [x, y, z]
* @returns {number} Distance between points
*/
calculateDistance(pos1, pos2) {
return Math.sqrt(
Math.pow(pos1[0] - pos2[0], 2) +
Math.pow(pos1[1] - pos2[1], 2) +
Math.pow(pos1[2] - pos2[2], 2)
);
}
/**
* Update villager state
* @param {DailyRoutineManager} routineManager - Routine manager
* @param {VillagePathfinder} pathfinder - Pathfinding system
* @param {number} deltaTime - Time since last update
*/
update(routineManager, pathfinder, deltaTime) {
// Update needs
this.updateNeeds(deltaTime);
// Get current activity
const activity = routineManager.getCurrentActivity(this.id);
// Update state based on activity
if (activity) {
this.state = activity.activity;
// If we have a location for this activity, pathfind to it
if (activity.location && this.calculateDistance(this.position, activity.location) > 1) {
this.path = pathfinder.findPath(this.position, activity.location);
} else {
this.path = [];
}
}
// Perform state-specific actions
switch (this.state) {
case 'sleep':
this.sleep(deltaTime);
break;
case 'work':
this.work(deltaTime);
break;
case 'eat':
this.eat(deltaTime);
break;
case 'socialize':
this.socialize(deltaTime);
break;
}
}
/**
* Update villager needs over time
* @param {number} deltaTime - Time since last update
*/
updateNeeds(deltaTime) {
this.energy = Math.max(0, this.energy - 0.1 * deltaTime);
this.hunger = Math.min(100, this.hunger + 0.05 * deltaTime);
this.socialNeed = Math.min(100, this.socialNeed + 0.03 * deltaTime);
}
/**
* Check if villager is tired
* @returns {boolean} True if tired
*/
isTired() {
return this.energy < 20;
}
/**
* Check if villager should work
* @returns {boolean} True if should work
*/
shouldWork() {
return this.energy > 30 && this.hunger < 80;
}
/**
* Check if villager should socialize
* @returns {boolean} True if should socialize
*/
shouldSocialize() {
return this.energy > 40 && this.socialNeed > 50;
}
/**
* Sleep action
* @param {number} deltaTime - Time since last update
*/
sleep(deltaTime) {
this.energy = Math.min(100, this.energy + 0.5 * deltaTime);
}
/**
* Work action
* @param {number} deltaTime - Time since last update
*/
work(deltaTime) {
this.energy = Math.max(0, this.energy - 0.2 * deltaTime);
}
/**
* Eat action
* @param {number} deltaTime - Time since last update
*/
eat(deltaTime) {
this.hunger = Math.max(0, this.hunger - 0.8 * deltaTime);
}
/**
* Socialize action
* @param {number} deltaTime - Time since last update
*/
socialize(deltaTime) {
this.socialNeed = Math.max(0, this.socialNeed - 0.6 * deltaTime);
}
/**
* Idle action
*/
idle() {
// Do nothing
}
}
export { DailyRoutineManager, Villager }; |