mirror of
https://github.com/Sheldan/canvas.git
synced 2026-01-06 08:20:50 +00:00
survivors: adding spread weapon
This commit is contained in:
@@ -62,6 +62,14 @@ export class Vector {
|
||||
return Math.acos(this.dotProduct2(vector) / (this.vecLength() * vector.vecLength()))
|
||||
}
|
||||
|
||||
rotate(angle: number) {
|
||||
let c = Math.cos(angle);
|
||||
let s = Math.sin(angle)
|
||||
let x = this._x * c - this._y * s
|
||||
let y = this._x * s + this._y * c
|
||||
return new Vector(x, y)
|
||||
}
|
||||
|
||||
get x(): number {
|
||||
return this._x;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import {Player} from "./Player.ts";
|
||||
import {Vector} from "./base.ts";
|
||||
import {BasicEnemy, ContainerEnemy, Enemy, HealthEnemy, ShootingEnemy} from "./Enemies.ts";
|
||||
import {HUD} from "./ui.ts";
|
||||
import {HomingPistol, Pistol} from "./weapons.ts";
|
||||
import {HomingPistol, Pistol, SpreadWeapon} from "./weapons.ts";
|
||||
import {MoneyDrop} from "./drop.ts";
|
||||
|
||||
|
||||
@@ -123,6 +123,7 @@ docReady(function () {
|
||||
|
||||
player.addWeapon(Pistol.generatePistol(world))
|
||||
player.addWeapon(HomingPistol.generateHomingPistol(world))
|
||||
player.addWeapon(SpreadWeapon.generateSpreadWeapon(world))
|
||||
hud = new HUD(world);
|
||||
|
||||
requestAnimationFrame(updateCanvas);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type {Weapon} from "./interfaces.ts";
|
||||
import {drawDot} from "./utils.ts";
|
||||
import {drawDot, toRad} from "./utils.ts";
|
||||
import {Player} from "./Player.ts";
|
||||
import {HomingProjectile, Projectile, ProjectileStats, StraightProjectile} from "./projectile.ts";
|
||||
import {World} from "./World.ts";
|
||||
@@ -155,6 +155,55 @@ export class Pistol extends RangeWeapon {
|
||||
}
|
||||
}
|
||||
|
||||
export class SpreadWeapon extends RangeWeapon {
|
||||
draw(ctx: CanvasRenderingContext2D) {
|
||||
drawDot(this.getPosition(), 1, this.color, ctx)
|
||||
}
|
||||
|
||||
private createProjectile(): boolean {
|
||||
let range = this.calculateRange()
|
||||
let closestTargetTo = this.world.getClosestTargetTo(this.world.player.position, range);
|
||||
if(closestTargetTo !== undefined && closestTargetTo[1] !== undefined) {
|
||||
let stats = new ProjectileStats()
|
||||
.withPiercings(this.stats.projectilePiercings)
|
||||
.withSize(1)
|
||||
.withDamage(this.stats.damage)
|
||||
.withSpeed(this.stats.projectileSpeed);
|
||||
let targetPosition = closestTargetTo[1]!.getPosition();
|
||||
let weaponPosition = this.getPosition();
|
||||
let mainVector = Vector.createVector(targetPosition, weaponPosition)
|
||||
let mainProjectile = StraightProjectile.createStraightProjectile(this.world, weaponPosition, targetPosition, this.player, stats, 'gray')
|
||||
this.projectiles.push(mainProjectile)
|
||||
let upperVector = mainVector.rotate(toRad(-30))
|
||||
let upperTarget = weaponPosition.add(upperVector.multiply(this.world.maxValue()))
|
||||
let upperProjectile = StraightProjectile.createStraightProjectile(this.world, weaponPosition, upperTarget, this.player, stats, 'gray')
|
||||
this.projectiles.push(upperProjectile)
|
||||
let lowerVector = mainVector.rotate(toRad(30))
|
||||
let lowerTarget = weaponPosition.add(lowerVector.multiply(this.world.maxValue()))
|
||||
let lowerProjectile = StraightProjectile.createStraightProjectile(this.world, weaponPosition, lowerTarget, this.player, stats, 'gray')
|
||||
this.projectiles.push(lowerProjectile)
|
||||
return true
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static generateSpreadWeapon(world: World, offset?: Vector) {
|
||||
if(!offset) {
|
||||
offset = new Vector(5, 5)
|
||||
}
|
||||
let stats = new WeaponStats()
|
||||
.withProjectileSpeed(1)
|
||||
.withDamage(3)
|
||||
.withShootInterval(40)
|
||||
let pistol = new SpreadWeapon(world, stats)
|
||||
pistol.offset = offset;
|
||||
pistol.size = 5;
|
||||
pistol.color = 'gray';
|
||||
return pistol;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export class WeaponStats {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user