Skip to content

Commit

Permalink
v0.8.3 fake 3D (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderclarktx authored Sep 27, 2024
1 parent e43cf0c commit bca19a8
Show file tree
Hide file tree
Showing 36 changed files with 237 additions and 186 deletions.
3 changes: 1 addition & 2 deletions core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ export * from "./src/ecs/actions/movement/Head"
export * from "./src/ecs/actions/movement/Move"
export * from "./src/ecs/actions/movement/WASDInputMap"
export * from "./src/ecs/actions/movement/VehicleMovement"
export * from "./src/ecs/entities/items/Axe"
export * from "./src/ecs/entities/items/GunItems"
export * from "./src/ecs/entities/items/Pickaxe"
export * from "./src/ecs/components/Controlling"
export * from "./src/sound/Sounds"
export * from "./src/utils/MathUtils"
Expand Down
37 changes: 23 additions & 14 deletions core/src/ecs/actions/PlayerActions.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
import { Action, Controlling, InvokedAction, Noob, Skelly, TeamColors, XY } from "@piggo-gg/core";
import { Action, Controlling, Inventory, InvokedAction, Noob, Skelly, TeamColors, XY } from "@piggo-gg/core"

export const controlEntity: Action = Action(({ entity, player }) => {
if (!entity || !player) return;
if (!entity || !player) return

player.components.controlling = Controlling({ entityId: entity.id });
player.components.controlling = Controlling({ entityId: entity.id })
})

export const invokeSpawnSkelly = (player: Noob, color?: number, pos?: XY): InvokedAction => ({
action: "spawnSkelly", playerId: player.id, params: { color, pos }
})

export const setActiveItemIndex = Action<{ index: number }>(({ params, entity }) => {
if (params.index === null || params.index === undefined) return

const inventory = entity?.components.inventory
if (!inventory) return

inventory.setActiveItemIndex(params.index)
})

export const spawnSkelly = Action<{ color: number, pos: XY }>(({ player, world, params }) => {
if (!player) return;
if (!player) return

const characterForPlayer = Skelly(`skelly-${player.id}`, player.components.team.data.team, params.color, params.pos);
player.components.controlling = Controlling({ entityId: characterForPlayer.id });
world.addEntity(characterForPlayer);
const characterForPlayer = Skelly(`skelly-${player.id}`, player.components.team.data.team, params.color, params.pos)
player.components.controlling = Controlling({ entityId: characterForPlayer.id })
world.addEntity(characterForPlayer)
})

export const switchTeam = Action(({ entity, world }) => {
if (!entity) return;
if (!entity) return

const { team, controlling } = entity.components;
if (!team) return;
const { team, controlling } = entity.components
if (!team) return

team.switchTeam();
team.switchTeam()

const character = controlling?.getControlledEntity(world);
const character = controlling?.getControlledEntity(world)
if (character) {
const { team, renderable } = character.components;
const { team, renderable } = character.components

if (team) team.switchTeam();
if (team) team.switchTeam()

if (team && renderable) {
renderable.prepareAnimations(TeamColors[team.data.team])
Expand Down
16 changes: 8 additions & 8 deletions core/src/ecs/actions/abilities/IceWall.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Action, LineWall, XY, playSound } from "@piggo-gg/core";
import { Action, LineWall, XY, abs, min, playSound, round, sign } from "@piggo-gg/core";

export const IceWall = Action<XY>(({ world, params, entity }) => {
if (!entity || !entity.components.position) return;
Expand All @@ -9,12 +9,12 @@ export const IceWall = Action<XY>(({ world, params, entity }) => {
const { x, y } = entity.components.position.data;

// distance to mouse
const dx = Math.abs(mouseX - x);
const dy = Math.abs(mouseY - y);
const dx = abs(mouseX - x);
const dy = abs(mouseY - y);

// sign
const sx = Math.sign(mouseX - x);
const sy = Math.sign(mouseY - y);
const sx = sign(mouseX - x);
const sy = sign(mouseY - y);

// flip X axis
const flip = sx === sy ? -1 : 1;
Expand All @@ -24,9 +24,9 @@ export const IceWall = Action<XY>(({ world, params, entity }) => {
const ry = dy / (dx + dy)

const coords = [
mouseX - flip * Math.min(width, (width * ry)), mouseY - Math.min(width, (width * rx)),
mouseX + flip * Math.min(width, (width * ry)), mouseY + Math.min(width, (width * rx))
].map(Math.round);
mouseX - flip * min(width, (width * ry)), mouseY - min(width, (width * rx)),
mouseX + flip * min(width, (width * ry)), mouseY + min(width, (width * rx))
].map(round);

world.addEntity(LineWall({ points: coords, visible: true, health: 30, shootable: false }));

Expand Down
6 changes: 3 additions & 3 deletions core/src/ecs/actions/attacks/Reload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Action, Effect, Gun } from "@piggo-gg/core"
import { Action, Effect, Gun, max, min } from "@piggo-gg/core"

export const Reload = Action(({ entity }) => {
if (!entity) return
Expand All @@ -24,8 +24,8 @@ const ReloadEffect = (gun: Gun): Effect => ({
onEnd: () => {
gun.reloading = false

const clip = Math.min(gun.data.clip + gun.data.ammo, gun.clipSize)
const ammo = Math.max(gun.data.ammo - gun.clipSize + gun.data.clip, 0)
const clip = min(gun.data.clip + gun.data.ammo, gun.clipSize)
const ammo = max(gun.data.ammo - gun.clipSize + gun.data.clip, 0)

// gun.data.clip = clip
gun.data.clip = gun.clipSize // TODO infinite ammo
Expand Down
5 changes: 3 additions & 2 deletions core/src/ecs/actions/movement/Point.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Action } from "@piggo-gg/core";
import { Action, XY } from "@piggo-gg/core";

export const Point = Action<{ pointing: number }>(({ params, entity }) => {
export const Point = Action<{ pointing: number, pointingDelta: XY }>(({ params, entity }) => {
if (!entity) return;

const { position } = entity.components;
if (!position) return;

position.data.pointing = params.pointing;
position.data.pointingDelta = params.pointingDelta;
});
40 changes: 32 additions & 8 deletions core/src/ecs/components/Gun.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, World, randomInt } from "@piggo-gg/core";
import { Actions, Component, Effects, Entity, Input, Item, Name, Reload, Shoot, SpawnBullet, World, randomInt } from "@piggo-gg/core";

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

Expand Down Expand Up @@ -30,7 +30,7 @@ export type GunProps = {
speed: number
}

const GunBuilder = (props: GunProps) => () => Gun(props);
const GunBuilder = (props: GunProps) => () => Gun(props)

export const Gun = (props: GunProps): Gun => {

Expand Down Expand Up @@ -81,7 +81,7 @@ export const Gun = (props: GunProps): Gun => {
return gun;
}

export const Deagle = GunBuilder({
const DeagleBuilder = GunBuilder({
name: "deagle",
automatic: false,
ammo: 60,
Expand All @@ -93,7 +93,7 @@ export const Deagle = GunBuilder({
speed: 400
});

export const AK = GunBuilder({
const AKBuilder = GunBuilder({
name: "ak",
automatic: true,
ammo: 90,
Expand All @@ -105,7 +105,7 @@ export const AK = GunBuilder({
speed: 500
});

export const AWP = GunBuilder({
const AWPBuilder = GunBuilder({
name: "awp",
automatic: false,
ammo: 20,
Expand All @@ -118,7 +118,31 @@ export const AWP = GunBuilder({
});

export const WeaponTable: Record<GunNames, () => Gun> = {
"deagle": Deagle,
"ak": AK,
"awp": AWP
"deagle": DeagleBuilder,
"ak": AKBuilder,
"awp": AWPBuilder
}

export const GunItem = (name: string, gun: () => Gun): Item => Entity({
id: name,
components: {
name: Name(name),
input: Input({
press: {
"r": ({ character, mouse }) => ({ action: "reload", params: { character, mouse } }),
"mb1": ({ character, mouse }) => ({ action: "shoot", params: { character, mouse } })
}
}),
actions: Actions<any>({
"spawnBullet": SpawnBullet,
"shoot": Shoot,
"reload": Reload
}),
gun: gun(),
effects: Effects()
}
})

export const Deagle = () => GunItem("deagle", WeaponTable["deagle"]);
export const AK = () => GunItem("ak", WeaponTable["ak"]);
export const AWP = () => GunItem("awp", WeaponTable["awp"]);
10 changes: 8 additions & 2 deletions core/src/ecs/components/Inventory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ export type Item = Entity<Name | Input | Actions | Effects>

export type Inventory = Component<"inventory"> & {
items: Item[]
activeItem: Item | null
activeItemIndex: number
activeItem: () => Item | null
setActiveItemIndex: (index: number) => void
}

export const Inventory = (items: Item[]): Inventory => {
const inventory: Inventory = {
type: "inventory",
items,
activeItem: items[0] ?? null
activeItemIndex: 0,
activeItem: () => inventory.items[inventory.activeItemIndex] ?? null,
setActiveItemIndex: (index: number) => {
inventory.activeItemIndex = index
}
}
return inventory
}
Expand Down
4 changes: 3 additions & 1 deletion core/src/ecs/components/Position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type Position = Component<"position", {
speed: number
rotation: number
pointing: number
pointingDelta: XY
heading: XY
velocityResets: number
}> & {
Expand Down Expand Up @@ -45,6 +46,7 @@ export const Position = (props: PositionProps = {}): Position => {
speed: props.speed ?? 0,
rotation: 0,
pointing: 0,
pointingDelta: { x: 0, y: 0 },
heading: { x: NaN, y: NaN },
velocityResets: props.velocityResets ?? 0
},
Expand All @@ -62,7 +64,7 @@ export const Position = (props: PositionProps = {}): Position => {
position.data.velocity.y = round(y * 100) / 100;

const rads = (Math.atan2(y, x) / Math.PI) * 4 + 4;
position.orientationRads = rads;
position.orientationRads = round(rads, 2);

if (x || y) position.orientation = orthoToDirection(round(rads) % 8);

Expand Down
18 changes: 14 additions & 4 deletions core/src/ecs/entities/characters/Skelly.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
Actions, Axe, Boost, Collider, DeagleItem, Debug,
AK, AWP, Actions, Boost, Collider, Deagle, Debug,
DefaultJoystickHandler, Effects, Entity, Head, Health,
IceWall, Input, Inventory, Move, Networked, Point, Position,
Renderable, Team, TeamNumber, WASDInputMap, XY, loadTexture
IceWall, Input, Inventory, Move, Networked, Pickaxe, Point, Position,
Renderable, Team, TeamNumber, WASDInputMap, XY, loadTexture,
setActiveItemIndex
} from "@piggo-gg/core"
import { AnimatedSprite } from "pixi.js"

Expand All @@ -16,13 +17,21 @@ export const Skelly = (id: string, team: TeamNumber, color?: number, pos?: XY) =
collider: Collider({ shape: "ball", radius: 8, mass: 600, shootable: true }),
health: Health({ health: 100 }),
team: Team(team),
inventory: Inventory([Axe(), DeagleItem()]),
inventory: Inventory([Pickaxe(), AK(), AWP(), Deagle()]),
input: Input({
press: {
...WASDInputMap.press,
"mb2": ({ mouse, world }) => ({ action: "head", playerId: world.client?.playerId(), params: { mouse } }),
"q": ({ mouse, world }) => ({ action: "wall", playerId: world.client?.playerId(), params: mouse }),
"e": ({ mouse, world }) => ({ action: "boost", playerId: world.client?.playerId(), params: mouse }),
"1": ({ }) => ({ action: "setActiveItemIndex", playerId: id, params: { index: 0 } }),
"2": ({ }) => ({ action: "setActiveItemIndex", playerId: id, params: { index: 1 } }),
"3": ({ }) => ({ action: "setActiveItemIndex", playerId: id, params: { index: 2 } }),
"4": ({ }) => ({ action: "setActiveItemIndex", playerId: id, params: { index: 3 } }),
"5": ({ }) => ({ action: "setActiveItemIndex", playerId: id, params: { index: 4 } }),
"6": ({ }) => ({ action: "setActiveItemIndex", playerId: id, params: { index: 5 } }),
"7": ({ }) => ({ action: "setActiveItemIndex", playerId: id, params: { index: 6 } }),
"8": ({ }) => ({ action: "setActiveItemIndex", playerId: id, params: { index: 7 } })
},
joystick: DefaultJoystickHandler
}),
Expand All @@ -32,6 +41,7 @@ export const Skelly = (id: string, team: TeamNumber, color?: number, pos?: XY) =
"move": Move,
"wall": IceWall,
"point": Point,
"setActiveItemIndex": setActiveItemIndex
}),
effects: Effects(),
renderable: Renderable({
Expand Down
4 changes: 2 additions & 2 deletions core/src/ecs/entities/characters/Zomi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Actions, Chase, Collider, Debug, Entity, Health, InvokedAction,
NPC, Networked, Position, PositionProps, Renderable, World, ZomiAttack,
closestEntity, positionDelta, loadTexture, round, randomInt
closestEntity, positionDelta, loadTexture, round, randomInt, max
} from "@piggo-gg/core";
import { AnimatedSprite } from "pixi.js";

Expand Down Expand Up @@ -38,7 +38,7 @@ export const Zomi = ({ id, color, positionProps = { x: 100, y: 100 } }: ZomiProp
const { health, maxHealth } = z.components.health!.data;

const ratio = round(health / maxHealth * 4);
r.color = colors[Math.max(ratio - 1, 0)];
r.color = colors[max(ratio - 1, 0)];
},
setup: async (r: Renderable) => {
const t = await loadTexture("chars.json")
Expand Down
21 changes: 0 additions & 21 deletions core/src/ecs/entities/items/GunItems.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Action, Actions, Character, Effects, Entity, Input, Item, KeyMouse, Name } from "@piggo-gg/core"

export const Axe = (): Item => Entity({
id: "axe",
export const Pickaxe = (): Item => Entity({
id: "pickaxe",
components: {
name: Name("axe"),
name: Name("pickaxe"),
input: Input({
press: {
"mb1": ({ character, mouse }) => ({ action: "whack", params: { character, mouse } })
Expand Down
2 changes: 1 addition & 1 deletion core/src/ecs/entities/terrain/FloorTiles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collider, Entity, Position, PositionProps, Renderable, Renderer, TeamColors, XY, round } from "@piggo-gg/core";
import { Collider, Entity, Position, PositionProps, Renderable, Renderer, TeamColors, XY } from "@piggo-gg/core";
import { Container, Graphics, Matrix, RenderTexture, Sprite } from "pixi.js";

export type FloorTilesProps = {
Expand Down
Loading

0 comments on commit bca19a8

Please # to comment.