-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move several useful modules to framework
- Loading branch information
1 parent
439a4be
commit d7d02ac
Showing
9 changed files
with
160 additions
and
174 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |