Skip to content

Commit

Permalink
chore: rename Event to KEvent (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel authored Jun 27, 2024
1 parent 930a0b3 commit 44057da
Show file tree
Hide file tree
Showing 18 changed files with 220 additions and 205 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ add([
- deprecated `kaboom()` in favor of `kaplay()` (you can still use `kaboom*`)
- deprecated `SpriteComp.curAnim()` in favor of `SpriteComp.getCurAnim().name`
- deprecated `fadeIn` component in favor of `OpacityComp.fadeIn()`
- deprecated `Event`, `EventHandler` and `EventController` in favor of `KEvent`,
`KEventHandler` and `KEventController`

### v3000.1.17

Expand Down
26 changes: 13 additions & 13 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import type {
import { map, Vec2 } from "./math";

import {
EventController,
EventHandler,
isEqOrIncludes,
KEventController,
KEventHandler,
overload2,
setHasOrIncludes,
} from "./utils";
Expand Down Expand Up @@ -109,7 +109,7 @@ export default (opt: {
isMouseMoved: false,
lastWidth: opt.canvas.offsetWidth,
lastHeight: opt.canvas.offsetHeight,
events: new EventHandler<{
events: new KEventHandler<{
mouseMove: [];
mouseDown: [MouseButton];
mousePress: [MouseButton];
Expand Down Expand Up @@ -432,7 +432,7 @@ export default (opt: {
}
}

function onResize(action: () => void): EventController {
function onResize(action: () => void): KEventController {
return state.events.on("resize", action);
}

Expand Down Expand Up @@ -507,38 +507,38 @@ export default (opt: {
return state.events.on("mouseRelease", (m) => m === mouse && action(m));
});

function onMouseMove(f: (pos: Vec2, dpos: Vec2) => void): EventController {
function onMouseMove(f: (pos: Vec2, dpos: Vec2) => void): KEventController {
return state.events.on(
"mouseMove",
() => f(mousePos(), mouseDeltaPos()),
);
}

function onCharInput(action: (ch: string) => void): EventController {
function onCharInput(action: (ch: string) => void): KEventController {
return state.events.on("charInput", action);
}

function onTouchStart(f: (pos: Vec2, t: Touch) => void): EventController {
function onTouchStart(f: (pos: Vec2, t: Touch) => void): KEventController {
return state.events.on("touchStart", f);
}

function onTouchMove(f: (pos: Vec2, t: Touch) => void): EventController {
function onTouchMove(f: (pos: Vec2, t: Touch) => void): KEventController {
return state.events.on("touchMove", f);
}

function onTouchEnd(f: (pos: Vec2, t: Touch) => void): EventController {
function onTouchEnd(f: (pos: Vec2, t: Touch) => void): KEventController {
return state.events.on("touchEnd", f);
}

function onScroll(action: (delta: Vec2) => void): EventController {
function onScroll(action: (delta: Vec2) => void): KEventController {
return state.events.on("scroll", action);
}

function onHide(action: () => void): EventController {
function onHide(action: () => void): KEventController {
return state.events.on("hide", action);
}

function onShow(action: () => void): EventController {
function onShow(action: () => void): KEventController {
return state.events.on("show", action);
}

Expand Down Expand Up @@ -590,7 +590,7 @@ export default (opt: {
function onGamepadStick(
stick: GamepadStick,
action: (value: Vec2) => void,
): EventController {
): KEventController {
return state.events.on(
"gamepadStick",
(a: string, v: Vec2) => a === stick && action(v),
Expand Down
8 changes: 4 additions & 4 deletions src/assets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event } from "./utils";
import { KEvent } from "./utils";

/**
* An asset is a resource that is loaded asynchronously.
Expand All @@ -7,9 +7,9 @@ export class Asset<D> {
loaded: boolean = false;
data: D | null = null;
error: Error | null = null;
private onLoadEvents: Event<[D]> = new Event();
private onErrorEvents: Event<[Error]> = new Event();
private onFinishEvents: Event<[]> = new Event();
private onLoadEvents: KEvent<[D]> = new KEvent();
private onErrorEvents: KEvent<[Error]> = new KEvent();
private onFinishEvents: KEvent<[]> = new KEvent();
constructor(loader: Promise<D>) {
loader.then((data) => {
this.loaded = true;
Expand Down
4 changes: 2 additions & 2 deletions src/components/draw/particles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Texture } from "../../gfx";
import { getKaboomContext } from "../../kaboom";
import { Color, lerp, map, Quad, Vec2, vec2 } from "../../math";
import type { Comp, ShapeType } from "../../types";
import { Event } from "../../utils";
import { KEvent } from "../../utils";

class Particle {
pos: Vec2;
Expand Down Expand Up @@ -64,7 +64,7 @@ export function particles(popt: ParticlesOpt, eopt: EmitterOpt): ParticlesComp {
let count = 0;
let time = 0;

const onEndEvents = new Event();
const onEndEvents = new KEvent();

return {
id: "particles",
Expand Down
12 changes: 6 additions & 6 deletions src/components/draw/sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
SpriteCurAnim,
SpriteData,
} from "../../types";
import { Event, EventController } from "../../utils";
import { KEvent, KEventController } from "../../utils";

/**
* The {@link sprite `sprite()`} component.
Expand Down Expand Up @@ -79,11 +79,11 @@ export interface SpriteComp extends Comp {
/**
* Register an event that runs when an animation is played.
*/
onAnimStart(action: (anim: string) => void): EventController;
onAnimStart(action: (anim: string) => void): KEventController;
/**
* Register an event that runs when an animation is ended.
*/
onAnimEnd(action: (anim: string) => void): EventController;
onAnimEnd(action: (anim: string) => void): KEventController;
/**
* @since v3000.0
*/
Expand Down Expand Up @@ -151,7 +151,7 @@ export function sprite(
// 1 - from small index to large index
// -1 - reverse
let curAnimDir: -1 | 1 | null = null;
const spriteLoadedEvent = new Event<[SpriteData]>();
const spriteLoadedEvent = new KEvent<[SpriteData]>();

if (!src) {
throw new Error(
Expand Down Expand Up @@ -437,14 +437,14 @@ export function sprite(
onAnimEnd(
this: GameObj<SpriteComp>,
action: (name: string) => void,
): EventController {
): KEventController {
return this.on("animEnd", action);
},

onAnimStart(
this: GameObj<SpriteComp>,
action: (name: string) => void,
): EventController {
): KEventController {
return this.on("animStart", action);
},

Expand Down
12 changes: 6 additions & 6 deletions src/components/level/agent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Vec2 } from "../../math";
import type { Comp, GameObj, PosComp } from "../../types";
import type { EventController } from "../../utils";
import type { KEventController } from "../../utils";
import type { TileComp } from "./tile";

/**
Expand All @@ -19,10 +19,10 @@ export interface AgentComp extends Comp {
isTargetReachable(): boolean;
isTargetReached(): boolean;
setTarget(target: Vec2): void;
onNavigationStarted(cb: () => void): EventController;
onNavigationNext(cb: () => void): EventController;
onNavigationEnded(cb: () => void): EventController;
onTargetReached(cb: () => void): EventController;
onNavigationStarted(cb: () => void): KEventController;
onNavigationNext(cb: () => void): KEventController;
onNavigationEnded(cb: () => void): KEventController;
onTargetReached(cb: () => void): KEventController;
}

/**
Expand All @@ -39,7 +39,7 @@ export function agent(opts: AgentCompOpt = {}): AgentComp {
let target: Vec2 | null = null;
let path: Vec2[] | null = null;
let index: number | null = null;
let navMapChangedEvent: EventController | null = null;
let navMapChangedEvent: KEventController | null = null;
return {
id: "agent",
require: ["pos", "tile"],
Expand Down
2 changes: 1 addition & 1 deletion src/components/level/patrol.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Vec2 } from "../../math";
import type { Comp, GameObj, PosComp, QueryOpt } from "../../types";
import type { Comp, GameObj, PosComp } from "../../types";

export interface PatrolComp extends Comp {
patrolSpeed: number;
Expand Down
4 changes: 2 additions & 2 deletions src/components/level/sentry.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getKaboomContext } from "../../kaboom";
import { Vec2 } from "../../math";
import type { Comp, GameObj, PosComp, QueryOpt } from "../../types";
import type { EventController } from "../../utils";
import type { KEventController } from "../../utils";
import { raycast } from "../draw/raycast";

/**
Expand Down Expand Up @@ -29,7 +29,7 @@ export interface SentryComp extends Comp {
/*
* Binds the event fired when objects of interest are spotted.
*/
onObjectsSpotted(cb: (objects: GameObj[]) => void): EventController;
onObjectsSpotted(cb: (objects: GameObj[]) => void): KEventController;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/components/misc/health.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Comp, GameObj } from "../../types";
import type { EventController } from "../../utils";
import type { KEventController } from "../../utils";

/**
* The {@link health `health()`} component.
Expand Down Expand Up @@ -36,19 +36,19 @@ export interface HealthComp extends Comp {
*
* @since v2000.1
*/
onHurt(action: (amount?: number) => void): EventController;
onHurt(action: (amount?: number) => void): KEventController;
/**
* Register an event that runs when heal() is called upon the object.
*
* @since v2000.1
*/
onHeal(action: (amount?: number) => void): EventController;
onHeal(action: (amount?: number) => void): KEventController;
/**
* Register an event that runs when object's HP is equal or below 0.
*
* @since v2000.1
*/
onDeath(action: () => void): EventController;
onDeath(action: () => void): KEventController;
}

export function health(
Expand Down Expand Up @@ -88,16 +88,16 @@ export function health(
onHurt(
this: GameObj,
action: (amount?: number) => void,
): EventController {
): KEventController {
return this.on("hurt", action);
},
onHeal(
this: GameObj,
action: (amount?: number) => void,
): EventController {
): KEventController {
return this.on("heal", action);
},
onDeath(this: GameObj, action: () => void): EventController {
onDeath(this: GameObj, action: () => void): KEventController {
return this.on("death", action);
},
inspect() {
Expand Down
30 changes: 15 additions & 15 deletions src/components/misc/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Comp } from "../../types";
import { Event, EventController } from "../../utils";
import { KEvent, KEventController } from "../../utils";

/**
* The {@link state `state()`} component.
Expand All @@ -24,26 +24,26 @@ export interface StateComp extends Comp {
from: string,
to: string,
action: () => void,
): EventController;
): KEventController;
/**
* Register event that runs once when enters a specific state. Accepts arguments passed from `enterState(name, ...args)`.
*/
onStateEnter: (
state: string,
action: (...args: any) => void,
) => EventController;
) => KEventController;
/**
* Register an event that runs once when leaves a specific state.
*/
onStateEnd: (state: string, action: () => void) => EventController;
onStateEnd: (state: string, action: () => void) => KEventController;
/**
* Register an event that runs every frame when in a specific state.
*/
onStateUpdate: (state: string, action: () => void) => EventController;
onStateUpdate: (state: string, action: () => void) => KEventController;
/**
* Register an event that runs every frame when in a specific state.
*/
onStateDraw: (state: string, action: () => void) => EventController;
onStateDraw: (state: string, action: () => void) => KEventController;
}

export function state(
Expand All @@ -60,10 +60,10 @@ export function state(
function initStateEvents(state: string) {
if (!events[state]) {
events[state] = {
enter: new Event(),
end: new Event(),
update: new Event(),
draw: new Event(),
enter: new KEvent(),
end: new KEvent(),
update: new KEvent(),
draw: new KEvent(),
};
}
}
Expand Down Expand Up @@ -122,23 +122,23 @@ export function state(
from: string,
to: string,
action: () => void,
): EventController {
): KEventController {
return on("enter", `${from} -> ${to}`, action);
},

onStateEnter(state: string, action: () => void): EventController {
onStateEnter(state: string, action: () => void): KEventController {
return on("enter", state, action);
},

onStateUpdate(state: string, action: () => void): EventController {
onStateUpdate(state: string, action: () => void): KEventController {
return on("update", state, action);
},

onStateDraw(state: string, action: () => void): EventController {
onStateDraw(state: string, action: () => void): KEventController {
return on("draw", state, action);
},

onStateEnd(state: string, action: () => void): EventController {
onStateEnd(state: string, action: () => void): KEventController {
return on("end", state, action);
},

Expand Down
6 changes: 3 additions & 3 deletions src/components/misc/timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { getKaboomContext } from "../../kaboom";
import { lerp } from "../../math";
import type {
Comp,
EventController,
GameObj,
KEventController,
LerpValue,
TimerController,
TweenController,
Expand All @@ -25,7 +25,7 @@ export interface TimerComp extends Comp {
*
* @since v3000.0
*/
loop(time: number, action: () => void): EventController;
loop(time: number, action: () => void): KEventController;
/**
* Tweeeeen! Note that this doesn't specifically mean tweening on this object's property, this just registers the timer on this object, so the tween will cancel with the object gets destroyed, or paused when obj.paused is true.
*
Expand Down Expand Up @@ -76,7 +76,7 @@ export function timer(): TimerComp {
},
};
},
loop(t: number, action: () => void): EventController {
loop(t: number, action: () => void): KEventController {
let curTimer: null | TimerController = null;
const newAction = () => {
// TODO: should f be execute right away as loop() is called?
Expand Down
Loading

0 comments on commit 44057da

Please sign in to comment.