Skip to content

Commit

Permalink
sound effects (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderclarktx authored Aug 21, 2024
1 parent 1c44822 commit 4708673
Show file tree
Hide file tree
Showing 16 changed files with 31 additions and 12 deletions.
1 change: 1 addition & 0 deletions core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export * from "./src/ecs/actions/VehicleMovement";
export * from "./src/ecs/actions/Wall";
export * from "./src/ecs/actions/WASDInputMap";
export * from "./src/ecs/components/Controlling";
export * from "./src/sound/Sounds";
export * from "./src/utils/MathUtils";
export * from "./src/utils/PixiUtils";
export * from "./src/utils/RapierUtils";
Expand Down
3 changes: 2 additions & 1 deletion core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@pixi/ui": "2.0.1",
"pixi-filters": "6.0.3",
"pixi.js": "8.1.5",
"react-hot-toast": "2.4.1"
"react-hot-toast": "2.4.1",
"tone": "15.0.4"
}
}
2 changes: 2 additions & 0 deletions core/src/ecs/actions/Shoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ export const Shoot = Action<{ id: number, mouse: XY, tick: number }>(({ world, p
onHit: onHitTeam(team.data.team, gun.damage)
}));

if (world.client?.sounds[gun.name].loaded) world.client.sounds[gun.name].restart().start();

// auto reload
if (gun.data.clip === 0) {
const reload = entity.components.actions?.actionMap["reload"];
Expand Down
9 changes: 5 additions & 4 deletions core/src/ecs/components/Gun.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Component, World } from "@piggo-gg/core";

export type GunNames = "deagle" | "ak" | "awp";

export type Gun = Component<"gun", { id: number, clip: number, ammo: number }> & {
automatic: boolean
bulletSize: number
Expand All @@ -8,7 +10,7 @@ export type Gun = Component<"gun", { id: number, clip: number, ammo: number }> &
fireRate: number
lastShot: number
outlineColor: number
name: string
name: GunNames
reloading: boolean
reloadTime: number
speed: number
Expand All @@ -23,7 +25,7 @@ export type GunProps = {
clipSize: number
damage: number
fireRate: number
name: string
name: GunNames
reloadTime: number
speed: number
}
Expand Down Expand Up @@ -110,8 +112,7 @@ export const AWP = GunBuilder({
speed: 600
});


export const WeaponTable: Record<string, () => Gun> = {
export const WeaponTable: Record<GunNames, () => Gun> = {
"deagle": Deagle,
"ak": AK,
"awp": AWP
Expand Down
4 changes: 2 additions & 2 deletions core/src/ecs/entities/ui/BuyMenu.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Action, Actions, Entity, Input, Position, Renderable, TwoPoints, WeaponTable, World, clickableClickedThisFrame, loadTexture, pixiGraphics, pixiText } from "@piggo-gg/core";
import { Action, Actions, Entity, GunNames, Input, Position, Renderable, TwoPoints, WeaponTable, World, clickableClickedThisFrame, loadTexture, pixiGraphics, pixiText } from "@piggo-gg/core";
import { ScrollBox } from "@pixi/ui";
import { OutlineFilter } from "pixi-filters";
import { Container, Sprite } from "pixi.js";
Expand Down Expand Up @@ -86,7 +86,7 @@ const cell = async (text: string, width: number, height: number, world: World):
const playerCharacter = world.client?.playerCharacter();
if (!playerCharacter) return;

const newGun = WeaponTable[text.toLowerCase()];
const newGun = WeaponTable[text.toLowerCase() as GunNames];
if (!newGun) return;

playerCharacter.components.gun = newGun();
Expand Down
4 changes: 3 additions & 1 deletion core/src/net/Client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Character, DelaySyncer, LobbyCreate, LobbyCreateRequest, LobbyJoin,
LobbyJoinRequest, NetClientSystem, NetMessageTypes, Noob,
RequestData, RequestTypes, Syncer, World, genPlayerId
RequestData, RequestTypes, Sounds, Syncer, World, genPlayerId
} from "@piggo-gg/core";

const servers = {
Expand All @@ -20,6 +20,7 @@ export type Client = {
lobbyId: string | undefined
lastLatency: number
lastMessageTick: number
sounds: Sounds
playerId: () => string
playerCharacter: () => Character | undefined
createLobby: (callback: Callback<LobbyCreate>) => void
Expand All @@ -45,6 +46,7 @@ export const Client = ({ world }: ClientProps): Client => {
lastLatency: 0,
lastMessageTick: 0,
lobbyId: undefined,
sounds: Sounds(),
playerId: () => {
return client.playerEntity.id;
},
Expand Down
3 changes: 1 addition & 2 deletions core/src/runtime/World.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
Client, Command, Entity, Game, GameBuilder,
InvokedAction, Renderer, SerializedEntity, values,
StateBuffer, System, SystemBuilder, SystemEntity,
keys,
StateBuffer, System, SystemBuilder, SystemEntity, keys
} from "@piggo-gg/core";

export type World = {
Expand Down
12 changes: 12 additions & 0 deletions core/src/sound/Sounds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { GunNames } from "@piggo-gg/core";
import { Player } from "tone";

export type Sounds = Record<GunNames, Player>;

const sound: (url: string) => Player = (url) => new Player({ url, volume: -30 }).toDestination();

export const Sounds = (): Sounds => ({
deagle: sound("pistol.mp3"),
ak: sound("ak.mp3"),
awp: sound("awp.mp3"),
})
Binary file added docs/ak.mp3
Binary file not shown.
Binary file added docs/awp.mp3
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/piggo-gg-min.js

Large diffs are not rendered by default.

Binary file added docs/pistol.mp3
Binary file not shown.
Binary file added web/res/ak.mp3
Binary file not shown.
Binary file added web/res/awp.mp3
Binary file not shown.
Binary file added web/res/pistol.mp3
Binary file not shown.
3 changes: 2 additions & 1 deletion web/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ module.exports = {
{ from: "./res/favicon.ico", to: () => "favicon.ico" },
{ from: "./res/*.svg", to: () => "[name].svg" },
{ from: "./res/*.png", to: () => "[name].png" },
{ from: "./res/*.json", to: () => "[name].json" }
{ from: "./res/*.json", to: () => "[name].json" },
{ from: "./res/*.mp3", to: () => "[name].mp3" }
],
}),
new webpack.optimize.LimitChunkCountPlugin({
Expand Down

0 comments on commit 4708673

Please sign in to comment.