2021-06-14 22:03:55 -04:00
|
|
|
import { Serializable } from 'frigid';
|
|
|
|
|
import faker from 'faker';
|
|
|
|
|
import log from './log.js';
|
2021-06-16 15:26:42 -04:00
|
|
|
import { Task } from './tasks/Task.js';
|
2021-06-16 16:22:33 -04:00
|
|
|
import Time, { Tickable } from './Time.js';
|
2021-06-16 15:26:42 -04:00
|
|
|
import { ChopTreeTask } from './tasks/ChopTreeTask.js';
|
2021-06-14 22:03:55 -04:00
|
|
|
import { Game } from './Game.js';
|
2021-06-16 15:26:42 -04:00
|
|
|
import { render } from './ui/UI.js';
|
|
|
|
|
import { Memory } from './Memory.js';
|
2021-06-19 18:23:44 -04:00
|
|
|
import { getTheme } from './ui/Theme.js';
|
2021-06-14 22:03:55 -04:00
|
|
|
|
|
|
|
|
const LABORS = {
|
2021-06-19 12:40:01 -04:00
|
|
|
CUT_TREE: Symbol('CUT_TREE'),
|
|
|
|
|
MINING: Symbol('CUT_TREE'),
|
2021-06-14 22:03:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const SKILLS = {
|
2021-06-19 12:40:01 -04:00
|
|
|
PICKAXE: Symbol('PICKAXE'),
|
|
|
|
|
HATCHET: Symbol('HATCHET')
|
2021-06-14 22:03:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// const STATUS = {
|
|
|
|
|
// IDLE: Symbol('IDLE')
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const energyScale = 0.1;
|
|
|
|
|
|
|
|
|
|
export class Pawn extends Serializable implements Tickable {
|
2021-06-19 12:40:01 -04:00
|
|
|
name: {
|
|
|
|
|
first: string,
|
|
|
|
|
last: string
|
|
|
|
|
};
|
|
|
|
|
job: Task;
|
|
|
|
|
awake: boolean;
|
|
|
|
|
sex: number;
|
|
|
|
|
|
|
|
|
|
energy: number;
|
|
|
|
|
fun: number;
|
|
|
|
|
|
|
|
|
|
age: number;
|
|
|
|
|
|
|
|
|
|
memories: Memory[];
|
|
|
|
|
|
|
|
|
|
async tick() {
|
|
|
|
|
this.age ++;
|
|
|
|
|
|
|
|
|
|
this.energy -= energyScale;
|
|
|
|
|
|
|
|
|
|
if(this.awake === false) {
|
|
|
|
|
this.energy += energyScale * 4;
|
|
|
|
|
if(this.energy >= 100) {
|
|
|
|
|
this.awake = true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if(this.job) {
|
|
|
|
|
this.job.doWork(1, this);
|
|
|
|
|
this.energy -= energyScale;
|
|
|
|
|
if(this.job?.completed) {
|
|
|
|
|
this.stopWorking();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const inactive = Game.current.board.tasks.filter(task => {
|
|
|
|
|
return task.worker === null;
|
|
|
|
|
});
|
|
|
|
|
if(inactive.length > 0) {
|
|
|
|
|
const task = inactive[0];
|
|
|
|
|
// const task = inactive[Math.floor(Math.random() * inactive.length)];
|
|
|
|
|
this.assignJob(task);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(this.energy <= 0) {
|
|
|
|
|
this.stopWorking();
|
|
|
|
|
this.awake = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get idle() {
|
|
|
|
|
return !this.job && this.awake;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctor() {
|
|
|
|
|
log.info('Pawn::ctor')
|
|
|
|
|
this.name ??= {
|
|
|
|
|
first: faker.name.firstName(),
|
|
|
|
|
last: faker.name.lastName()
|
|
|
|
|
};
|
|
|
|
|
if(!this.sex) {
|
|
|
|
|
this.sex = Math.round(Math.random());
|
|
|
|
|
this.name.first = faker.name.firstName(this.sex);
|
|
|
|
|
}
|
|
|
|
|
this.awake ??= true;
|
|
|
|
|
this.energy ??= 100;
|
|
|
|
|
this.memories ??= [];
|
|
|
|
|
if(!this.age) {
|
|
|
|
|
this.age = Math.floor(525600 * (16 + Math.random() * 9));
|
|
|
|
|
this.memories.push({
|
|
|
|
|
type: "birth",
|
|
|
|
|
location: Game.current.name,
|
|
|
|
|
time: {
|
|
|
|
|
age: 0,
|
|
|
|
|
locale: new Time(Game.current.clock.stamp - this.age).toString()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(this.job?.completed) {
|
|
|
|
|
this.stopWorking();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stopWorking() {
|
|
|
|
|
if(this.job) {
|
|
|
|
|
this.job.stopJob();
|
|
|
|
|
this.job = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assignJob(task: Task) {
|
|
|
|
|
this.job?.stopJob()
|
|
|
|
|
this.job = task;
|
|
|
|
|
this.job.claim(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
|
if(this.job) {
|
|
|
|
|
return this.job.status;
|
|
|
|
|
} else {
|
2021-06-19 18:23:44 -04:00
|
|
|
return this.awake ? getTheme().status.idle('IDLE') : getTheme().status.self('RESTING')
|
2021-06-19 12:40:01 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static serializationDependencies() {
|
|
|
|
|
return [Task, ChopTreeTask]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
|
if(this.name) {
|
|
|
|
|
return this.name.first + ' ' + this.name.last;
|
|
|
|
|
} else {
|
|
|
|
|
return '[Object Pawn]';
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-14 22:03:55 -04:00
|
|
|
}
|