Skip to content

Instantly share code, notes, and snippets.

@al-arz
al-arz / changeParent.ts
Created August 22, 2024 13:20
A common task in pixi.js games is to change the parent of a Container (move it within the scene graph) without altering its on-screen position. Here's the snippet for that.
import Container from "pixi.js";
function getLocalPos(target: Container, parent: Container): void {
return parent.toLocal(target.parent.toGlobal(target.position));
}
// Change target's parent but preserve its screen position
function changeParent(target: Container, parent: Container): void {
const position = getLocalPos(target, parent);
parent.addChild(target);
@al-arz
al-arz / DungeonController.ts
Created August 22, 2024 13:01
An excerpt of player movement processing logic in a turn-based roguelite game, utilizing chain of responisbility pattern.
// Somewhere within the main gameplay controller class
private processHeroMove(nextPos: GlobalGridCoords) {
const interaction = new InteractionHandler(gameState);
const movement = new MovementHandler(gameState);
const combat = new CombatHandler(gameState);
const start = interaction;
interaction.next = movement;
movement.next = combat;
import { DisplayObject } from "@pixi/display";
export class Pool<T extends DisplayObject> {
private inactive: T[] = [];
public active: T[] = [];
private readonly createObject: () => T;
private readonly resetObject: (elem: T) => void;
private readonly destroyObject: (elem: T) => void;
constructor(
@al-arz
al-arz / dodbook-style
Last active June 23, 2023 09:41
Custom CSS for Data-Oriented Design book online version
/* Random CSS to make HTML version of Data-Oriented Design book (https://www.dataorienteddesign.com/dodbook/) a bit more pleasant to read on my 15" FHD laptop */
body {
font-family: 'Lato', "Verdana", sans-serif;
font-size: 1.4em;
line-height: 1.5em;
padding: 20px;
background-color: #eee;
color: #222
}