Skip to content

Commit

Permalink
Move several useful modules to framework
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelpicosean committed Jul 22, 2021
1 parent 439a4be commit d7d02ac
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 174 deletions.
38 changes: 0 additions & 38 deletions assets/extension/2d/vector_graphic.gd

This file was deleted.

2 changes: 1 addition & 1 deletion src/engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export * from 'engine/core/math/math_defs';
export * from 'engine/core/math/math_funcs';
export * from 'engine/core/math/vector2';
export * from 'engine/core/math/vector3';
// export * from 'engine/core/math/pool_vector2_array';
export * from 'engine/core/math/pool_vector2_array';
export * from 'engine/core/math/rect2';
export * from 'engine/core/math/aabb';
export * from 'engine/core/math/transform_2d';
Expand Down
133 changes: 0 additions & 133 deletions src/extension/2d/vector_graphic.ts

This file was deleted.

50 changes: 50 additions & 0 deletions src/framework/behavior.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export class Behavior<T> {
name: string;

runner: BehaviorRunner<T>;
target: T;

_begin_play() { }
_update(delta: number) { }
}

type BehaviorConstructor<T> = new () => Behavior<T>;

export class BehaviorRunner<T> {
target: T;

behaviors = new Map<BehaviorConstructor<T>, Behavior<T>>();

constructor(target: T, behaviors: BehaviorConstructor<T>[]) {
this.target = target;

for (let c of behaviors) {
this.add(c);
}
}

add<B extends Behavior<T>>(behv: new () => B): B {
const inst = new behv;
inst.runner = this;
inst.target = this.target;
this.behaviors.set(behv, inst);
return inst;
}
has<B extends Behavior<T>>(behv: new () => B): boolean {
return this.behaviors.has(behv);
}
get<B extends Behavior<T>>(behv: new () => B): B {
return this.behaviors.get(behv) as B;
}

begin_play() {
for (let [_, c] of this.behaviors) {
c._begin_play();
}
}
update(delta: number) {
for (let [_, c] of this.behaviors) {
c._update(delta);
}
}
}
4 changes: 2 additions & 2 deletions src/extension/ui/input.ts → src/framework/html_input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as v from 'engine/index';

export class Input extends v.Node2D {
export class HTMLInput extends v.Node2D {
width = 100;
height = 20;

Expand Down Expand Up @@ -88,4 +88,4 @@ export class Input extends v.Node2D {
}
}

v.attach_script("res://scene/ui/input.tscn", Input);
v.attach_script("res://scene/ui/input.tscn", HTMLInput);
34 changes: 34 additions & 0 deletions src/framework/instance_factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as v from "engine/index";

export function create_instance(url: string): v.Node {
const pool = inst_pool.get(url);
if (!pool || pool.length === 0) {
const inst = v.instanciate_scene(url);
inst.set_filename(url);
return inst;
}

const inst = pool.pop();
inst.request_ready();

return inst;
}

export function destroy_instance(inst: v.Node) {
const parent = inst.get_parent();
if (parent) {
parent.remove_child(inst);
}

if (!inst.filename) return;

let pool = inst_pool.get(inst.filename);
if (!pool) {
pool = new v.NoShrinkArray<v.Node>();
inst_pool.set(inst.filename, pool);
}

pool.push(inst);
}

const inst_pool = new Map<string, v.NoShrinkArray<v.Node>>();
File renamed without changes.
File renamed without changes.
73 changes: 73 additions & 0 deletions src/framework/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
export class State<T> {
name: string;

runner: StateRunner<T>;
target: T;

_reset() { }

_enter() { }
_update(delta: number) { }
_exit() { }
}

export class StateRunner<T> {
target: T;

global: State<T> = null;

curr: State<T> = null;
prev: State<T> = null;

next: new() => State<T> = null;

state_instances = new Map<new() => State<T>, State<T>>();

constructor(target: T) {
this.target = target;
}

set_target(target: T) {
this.target = target;
}

set_global_state(state: new() => State<T>) {
this.global = new state;
this.global.runner = this;
this.global.target = this.target;
this.global._enter();
}

change_state(state: new() => State<T>) {
this.next = state;
}

update(delta: number) {
if (this.global) {
this.global._update(delta);
}

if (this.next) {
if (this.curr) {
this.prev = this.curr;
this.prev._exit();
}

this.curr = this.state_instances.get(this.next);
if (!this.curr) {
this.curr = new (this.next);
this.state_instances.set(this.next, this.curr);
} else {
this.curr._reset();
}

this.curr.runner = this;
this.curr.target = this.target;
this.next = null;

this.curr._enter();
} else {
this.curr._update(delta);
}
}
}

0 comments on commit d7d02ac

Please sign in to comment.