Skip to content

Commit

Permalink
projectiles and weapon 1 ready
Browse files Browse the repository at this point in the history
  • Loading branch information
androettop committed Nov 2, 2024
1 parent 6f30484 commit 3603718
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 26 deletions.
29 changes: 13 additions & 16 deletions src/actors/player/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,40 +90,37 @@ class Player extends Actor {
}

private playerInput(engine: Engine) {
let newDirection = 0;

// wasd movement with keys
if (
GAME_CONTROLS.MOVE_RIGHT.some((key) => engine.input.keyboard.isHeld(key))
) {
newDirection += 1;
}

if (
this.direction = 1;
this.isRunning = true;
} else if (
GAME_CONTROLS.MOVE_LEFT.some((key) => engine.input.keyboard.isHeld(key))
) {
newDirection -= 1;
}

if (newDirection !== 0) {
this.direction = newDirection > 0 ? 1 : -1;
this.direction = -1;
this.isRunning = true;
} else {
this.isRunning = false;
}

// weapon switching
// jump
this.wantsJump = GAME_CONTROLS.JUMP.some((key) =>
engine.input.keyboard.isHeld(key),
);

// weapon switching
GAME_CONTROLS.WEAPON_SWITCH.forEach((keys, index) => {
if (keys.some((key) => engine.input.keyboard.wasPressed(key))) {
this.activeWeapon = index;
}
});

// jump
this.wantsJump = GAME_CONTROLS.JUMP.some((key) =>
engine.input.keyboard.isHeld(key),
);
// weapon shooting
if (GAME_CONTROLS.SHOOT.some((key) => engine.input.keyboard.isHeld(key))) {
this._activeWeapon?.shoot();
}
}

private updateGroundedState() {
Expand Down
33 changes: 33 additions & 0 deletions src/actors/projectile/p_bullet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Engine } from "excalibur";
import Projectile, { ProjectileParams } from "./projectile";
import { projectile01Sprite } from "./resources";

abstract class PBullet extends Projectile {
speed: number = 400;

constructor(params: ProjectileParams) {
super({
width: 16,
height: 16,
...params,
});
}

public destroy(offScreen: boolean = false) {
if (!offScreen) {
// Create particle effect
}
this.kill();
}

public update(engine: Engine, delta: number): void {
super.update(engine, delta);
}

public onInitialize(engine: Engine) {
super.onInitialize(engine);
this.graphics.use(projectile01Sprite);
}
}

export default PBullet;
42 changes: 42 additions & 0 deletions src/actors/projectile/projectile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Actor, ActorArgs, CollisionType, Engine, vec } from "excalibur";

export interface ProjectileParams extends ActorArgs {
/**
* The angle in radians to fire the projectile
*/
directionAngle: number;
}

abstract class Projectile extends Actor {
abstract speed: number;
private directionAngle: number;

constructor({ directionAngle, ...rest }: ProjectileParams) {
super({
pos: vec(0, 0),
...rest,
});
this.directionAngle = directionAngle;
}

abstract destroy(offScreen: boolean): void;

public update(engine: Engine, delta: number): void {
super.update(engine, delta);
if (this.isOffScreen) {
this.destroy(true);
}
}

public onInitialize(engine: Engine) {
super.onInitialize(engine);
this.body.collisionType = CollisionType.Passive;
this.vel = vec(
this.speed * Math.cos(this.directionAngle),
this.speed * Math.sin(this.directionAngle),
);
this.rotation = this.directionAngle;
}
}

export default Projectile;
9 changes: 9 additions & 0 deletions src/actors/projectile/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ImageSource } from "excalibur";
import bulletSprite from "../../assets/images/weapons/bullet_01.png";

export const ProjectileResources = {
Projectile01Sprite: new ImageSource(bulletSprite),
} as const;

export const projectile01Sprite =
ProjectileResources.Projectile01Sprite.toSprite();
44 changes: 42 additions & 2 deletions src/actors/weapons/weapon.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { Actor, ActorArgs, CollisionType, Engine, vec } from "excalibur";
import {
Actor,
ActorArgs,
CollisionType,
Engine,
vec,
Vector,
} from "excalibur";
import Projectile from "../projectile/projectile";
import Player from "../player/player";

export interface WeaponParams extends ActorArgs {}

abstract class Weapon extends Actor {
abstract readonly twoHanded: boolean;
abstract readonly label: string;
abstract readonly projectileType: typeof Projectile;
abstract readonly weaponSize: Vector;
abstract readonly shootThrottle: number;

private isWeaponShooting = false;

constructor(params?: WeaponParams) {
super({
Expand All @@ -15,7 +29,33 @@ abstract class Weapon extends Actor {
});
}

abstract shoot(): void;
createProjectile() {
const player = this.parent as Player | null;
if (!player) {
return;
}
console.log(player);
const ProjectileClass = this.projectileType;

// @ts-ignore: The projectile will extend the Projectile class.
const projectile = new ProjectileClass({
pos: player.pos
.clone()
.add(vec(this.weaponSize.x * player.direction, this.weaponSize.y)),
directionAngle: player.direction < 0 ? Math.PI : 0,
});
this.scene?.add(projectile);
}

shoot() {
if (!this.isWeaponShooting) {
this.createProjectile();
this.isWeaponShooting = true;
this.actions.delay(this.shootThrottle).callMethod(() => {
this.isWeaponShooting = false;
});
}
}

public onInitialize(engine: Engine) {
super.onInitialize(engine);
Expand Down
9 changes: 5 additions & 4 deletions src/actors/weapons/wp_pistol.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Weapon from "./weapon";
import Player from "../player/player";
import { weapon01SpriteSheet } from "./resources";
import PBullet from "../projectile/p_bullet";
import { vec } from "excalibur";

class WpPistol extends Weapon {
public readonly twoHanded = false;
public readonly label = "Pistol";

shoot() {
console.log("shoot");
}
public readonly projectileType = PBullet;
public readonly weaponSize = vec(21, 0);
public readonly shootThrottle = 1000;

public animateWeapon() {
const player = this.parent as Player | null;
Expand Down
9 changes: 5 additions & 4 deletions src/actors/weapons/wp_shotgun.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Weapon from "./weapon";
import Player from "../player/player";
import { weapon02SpriteSheet } from "./resources";
import PBullet from "../projectile/p_bullet";
import { vec } from "excalibur";

class WpShotgun extends Weapon {
public readonly twoHanded = true;
public readonly label = "Shot Gun";

shoot() {
console.log("shoot");
}
public readonly projectileType = PBullet;
public readonly weaponSize = vec(20, 6);
public readonly shootThrottle = 100;

public animateWeapon() {
const player = this.parent as Player | null;
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SoundResources } from "./actors/sounds/resources";
import { WorldResources } from "./actors/world/resources";
import { PlayerResources } from "./actors/player/resources";
import { TextResources } from "./actors/text/resources";
import { ProjectileResources } from "./actors/projectile/resources";

class Game extends Engine {
constructor() {
Expand All @@ -32,6 +33,7 @@ class Game extends Engine {
WorldResources,
PlayerResources,
TextResources,
ProjectileResources,
),
);
}
Expand Down

0 comments on commit 3603718

Please sign in to comment.