hadean-old/src/Pawn.ts

144 lines
3.0 KiB
TypeScript
Raw Normal View History

2021-06-14 22:03:55 -04:00
import { Serializable } from 'frigid';
import faker from 'faker';
2021-06-22 19:25:41 -04:00
import { Task } from './registries/Tasks.js';
2021-06-16 16:22:33 -04:00
import Time, { Tickable } from './Time.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-22 19:25:41 -04:00
import { getTheme } from './registries/Themes.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() {
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 {
return this.awake ? getTheme().status.idle('IDLE') : getTheme().status.self('RESTING')
2021-06-19 12:40:01 -04:00
}
}
static serializationDependencies() {
2021-06-22 19:25:41 -04:00
return [Task]
2021-06-19 12:40:01 -04:00
}
toString() {
if(this.name) {
return this.name.first + ' ' + this.name.last;
} else {
return '[Object Pawn]';
}
}
2021-06-14 22:03:55 -04:00
}