Skip to content

Commit

Permalink
初步计划后续以显示对象为基本显示单位
Browse files Browse the repository at this point in the history
  • Loading branch information
hxg2050 committed Jun 10, 2023
1 parent 29d888a commit aedea27
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
25 changes: 19 additions & 6 deletions example/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { Node, Sprite, Transform } from "../src";
import { Application, Node, Sprite, Transform, canvas2d } from "../src";
/**
* 创建应用
*/
const app = new Application({
width: 1280,
height: 720
});

const canvas = document.querySelector('#canvas')! as HTMLCanvasElement;
app.use(canvas2d(canvas));


class User extends Node {

sprite = new Sprite();

foot = new Node();

constructor() {
super(Sprite);
this.addComponent(this.sprite);
this.addChild(this.foot);
}
start(): void {
this.addChild(new Transform());
console.log('start');
}
}

render() {

}
}
app.stage.addChild(new User);
console.log(app);
Empty file.
9 changes: 7 additions & 2 deletions src/core/transform/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { Container, Sprite } from "../component";
import { Constructor, Transform } from "./Transform";

export class Node<T extends Container = Container> extends Transform<T> {

// 要显示的对象
display?: T;

constructor(classConstructor?: Constructor<T>) {
super(classConstructor);
this.render && this.render();
this.display = this.container;
this.start && setTimeout(this.start.bind(this));
}

render?();
start?();
}
7 changes: 3 additions & 4 deletions src/core/transform/Transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ export class Transform<T extends Container = Container> {
*/
constructor(classConstructor?: Constructor<T>) {
this.id = ++ id;
classConstructor && this.addComponent(classConstructor);
if (classConstructor) {
this.container = this.addComponent(classConstructor);
}
}

private _position: Vector2 = new Vector2();
Expand Down Expand Up @@ -186,7 +188,6 @@ export class Transform<T extends Container = Container> {

// 要应用位置等信息的元素
container!: T;

/**
* 所有组件
* 默认包含一个容器组件,当添加了其他容器组件后自动替换,只能包含一个视觉组件
Expand Down Expand Up @@ -224,8 +225,6 @@ export class Transform<T extends Container = Container> {
*/
alpha = 1;

start?(): void;

/**
* 添加一个组件
* @param classConstructor - 要挂载的组件
Expand Down

0 comments on commit aedea27

Please sign in to comment.