mirror of
https://github.com/Sheldan/canvas.git
synced 2026-04-15 12:10:20 +00:00
survivors: reducing jumpiness of homing projectiles re-directing by limiting it to a certain angle
adding rad degree conversion utils checking world border for projectiles
This commit is contained in:
@@ -62,6 +62,10 @@ export class World {
|
|||||||
return this._size;
|
return this._size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
outside(position: Vector): boolean {
|
||||||
|
return position.x > this.size.x || position.y > this.size.y || position.x < 0 || position.y < 0
|
||||||
|
}
|
||||||
|
|
||||||
addEnemy(enemy: Enemy) {
|
addEnemy(enemy: Enemy) {
|
||||||
this._enemies.push(enemy)
|
this._enemies.push(enemy)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import type {Placeable} from "./interfaces.ts";
|
||||||
|
|
||||||
export class Vector {
|
export class Vector {
|
||||||
|
|
||||||
constructor(private _x: number, private _y: number) {
|
constructor(private _x: number, private _y: number) {
|
||||||
@@ -44,6 +46,13 @@ export class Vector {
|
|||||||
return this.multiply(-1)
|
return this.multiply(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dotProduct(vector: Vector): number {
|
||||||
|
return this._x * vector._x + this._y * vector._y;
|
||||||
|
}
|
||||||
|
|
||||||
|
angleTo(vector: Vector): number {
|
||||||
|
return Math.acos(this.dotProduct(vector))
|
||||||
|
}
|
||||||
|
|
||||||
get x(): number {
|
get x(): number {
|
||||||
return this._x;
|
return this._x;
|
||||||
@@ -88,3 +97,24 @@ export class Cooldown {
|
|||||||
this._currentValue = this._totalValue;
|
this._currentValue = this._totalValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class Point implements Placeable {
|
||||||
|
|
||||||
|
private position: Vector;
|
||||||
|
|
||||||
|
|
||||||
|
constructor(position: Vector) {
|
||||||
|
this.position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
getPosition(): Vector {
|
||||||
|
return this.position;
|
||||||
|
}
|
||||||
|
|
||||||
|
getSize() {
|
||||||
|
}
|
||||||
|
|
||||||
|
move(any?: any) {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import type {Acting, Placeable, Healthy} from "./interfaces.ts";
|
import type {Acting, Placeable, Healthy} from "./interfaces.ts";
|
||||||
import type {Vector} from "./base.ts";
|
import type {Vector} from "./base.ts";
|
||||||
import {World} from "./World.ts";
|
import {World} from "./World.ts";
|
||||||
import {Cooldown, Vector} from "./base.ts";
|
import {Cooldown, Point, Vector} from "./base.ts";
|
||||||
import {drawDot, moveInDirectionOf, straightMove} from "./utils.ts";
|
import {drawDot, moveInDirectionOf, straightMove, toRad} from "./utils.ts";
|
||||||
import {InstanceOfUtils} from "./instance.ts";
|
import {InstanceOfUtils} from "./instance.ts";
|
||||||
|
|
||||||
export abstract class Projectile implements Acting, Placeable {
|
export abstract class Projectile implements Acting, Placeable {
|
||||||
@@ -48,7 +48,13 @@ export abstract class Projectile implements Acting, Placeable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.status.collisionCooldown.decreaseCooldown();
|
this.status.collisionCooldown.decreaseCooldown();
|
||||||
|
this.checkWorldBorder()
|
||||||
|
}
|
||||||
|
|
||||||
|
checkWorldBorder() {
|
||||||
|
if(this.world.outside(this.position)) {
|
||||||
|
this.world.removeProjectile(this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impactPlayer() {
|
impactPlayer() {
|
||||||
@@ -97,7 +103,6 @@ export class HomingProjectile extends Projectile {
|
|||||||
|
|
||||||
private target: Placeable;
|
private target: Placeable;
|
||||||
|
|
||||||
|
|
||||||
constructor(position: Vector, speedVec: Vector, stats: ProjectileStats, world: World, parent: any, target: Placeable) {
|
constructor(position: Vector, speedVec: Vector, stats: ProjectileStats, world: World, parent: any, target: Placeable) {
|
||||||
super(position, speedVec, stats, world, parent);
|
super(position, speedVec, stats, world, parent);
|
||||||
this.target = target;
|
this.target = target;
|
||||||
@@ -112,12 +117,22 @@ export class HomingProjectile extends Projectile {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let closestTargetTo = this.world.getClosestTargetTo(this.world.player.position)
|
let closestTargetTo = this.world.getClosestTargetTo(this.world.player.position)
|
||||||
|
|
||||||
if (closestTargetTo !== undefined && closestTargetTo[1] !== undefined) {
|
if (closestTargetTo !== undefined && closestTargetTo[1] !== undefined) {
|
||||||
this.target = closestTargetTo[1]!;
|
let dir = Vector.createVector(this.target.getPosition(), this.position).normalize()
|
||||||
|
let newTargetPosition = closestTargetTo[1]!.getPosition();
|
||||||
|
let newDir = Vector.createVector(newTargetPosition, this.position).normalize()
|
||||||
|
let newDirAngle = newDir.angleTo(dir);
|
||||||
|
if(Math.abs(newDirAngle) < toRad(60)) {
|
||||||
|
this.target = closestTargetTo[1]!;
|
||||||
|
} else {
|
||||||
|
this.target = new Point(this.target.getPosition().add(dir.normalize().multiply(Math.max(this.world.size.x, this.world.size.y))))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.position = moveInDirectionOf(this.position, this.target.getPosition(), this.speedVec.vecLength())
|
this.position = moveInDirectionOf(this.position, this.target.getPosition(), this.speedVec.vecLength())
|
||||||
|
this.checkWorldBorder()
|
||||||
}
|
}
|
||||||
|
|
||||||
static createHomingProjectile(world: World, start: Vector, parent: any, target: Placeable, stats: ProjectileStats, color?: string) {
|
static createHomingProjectile(world: World, start: Vector, parent: any, target: Placeable, stats: ProjectileStats, color?: string) {
|
||||||
|
|||||||
@@ -16,3 +16,11 @@ export function moveInDirectionOf(position: Vector, target: Vector, speedFactor:
|
|||||||
export function straightMove(position: Vector, speed: Vector): Vector {
|
export function straightMove(position: Vector, speed: Vector): Vector {
|
||||||
return position.add(speed)
|
return position.add(speed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function toRad(angle) {
|
||||||
|
return angle / 180 * Math.PI;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function toDegrees(angle) {
|
||||||
|
return angle * 180 / Math.PI
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user