survivors: adding health enemies dropping health packs

This commit is contained in:
Sheldan
2025-08-21 18:36:31 +02:00
parent 71f48404c9
commit 8ca64a19b7
5 changed files with 123 additions and 33 deletions

View File

@@ -3,27 +3,17 @@ import {World} from "./World.ts";
import {drawDot, moveInDirectionOf} from "./utils.ts";
import {Vector} from "./base.ts";
export class MoneyDrop implements Drop {
private world: World;
private worth: number;
private _position: Vector;
private _color: string;
private _size: number;
export abstract class BasicDrop implements Drop {
protected world: World;
protected _position: Vector;
protected _color: string;
protected _size: number;
constructor(world: World, position: Vector) {
this.world = world;
this._position = position.clone();
}
draw(ctx: CanvasRenderingContext2D) {
drawDot(this._position, this.getSize(), this._color, ctx)
}
pickup() {
this.world.player.status.wealth += this.worth
}
getPosition(): Vector {
return this._position;
}
@@ -31,6 +21,41 @@ export class MoneyDrop implements Drop {
move() {
}
pickup() {
}
act() {
let distanceToPlayer = this._position.distanceTo(this.world.player.position);
if(distanceToPlayer < (this.world.player.stats.size + this._size)) {
this.pickup()
this.world.removeDrop(this)
} else if(distanceToPlayer < this.world.player.stats.pullRange) {
let speedFactor = 125 / distanceToPlayer;
this._position = moveInDirectionOf(this._position, this.world.player.position, speedFactor)
}
}
getSize() {
return this._size
}
abstract draw(ctx: CanvasRenderingContext2D);
}
export class MoneyDrop extends BasicDrop {
private worth: number;
draw(ctx: CanvasRenderingContext2D) {
drawDot(this._position, this.getSize(), this._color, ctx)
}
pickup() {
this.world.player.status.wealth += this.worth
}
static createMoneyDrop(world: World, position?: Vector): MoneyDrop {
if(!position) {
position = world.randomPlace()
@@ -43,19 +68,29 @@ export class MoneyDrop implements Drop {
return drop;
}
act() {
let distanceToPlayer = this._position.distanceTo(this.world.player.position);
if(distanceToPlayer < (this.world.player.stats.size + this._size)) {
this.pickup()
this.world.removeDrop(this)
} else if(distanceToPlayer < this.world.player.stats.pullRange) {
let speedFactor = 125 / distanceToPlayer;
this._position = moveInDirectionOf(this._position, this.world.player.position, speedFactor)
}
export class HealthPack extends BasicDrop {
private healAmount: number;
draw(ctx: CanvasRenderingContext2D) {
drawDot(this._position, this.getSize(), this._color, ctx)
}
pickup() {
this.world.player.heal(this.healAmount)
}
static createHealthPack(world: World, position?: Vector): HealthPack {
if(!position) {
position = world.randomPlace()
}
let drop = new HealthPack(world, position)
drop.healAmount = 5;
drop._size = 2;
drop._color = 'green';
world.addDrop(drop)
return drop;
}
getSize() {
return this._size
}
}