Compare commits
76 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dccd4e21b6 | |||
| c96e8b3a04 | |||
| e686ba64d7 | |||
| 14cb9cd257 | |||
| 13e7737cb9 | |||
| dd65c60921 | |||
| 7dffb4d94a | |||
| 476204a296 | |||
| 983c8fbc99 | |||
| c30e591f6e | |||
| d9a1b5578c | |||
| f0e04b6981 | |||
| 14598f08c7 | |||
| 032b293085 | |||
| d9bb76c105 | |||
| 20392c8ab6 | |||
| 583e03d025 | |||
| f6c2d81a83 | |||
| e703ff4e6c | |||
| 877fc4c9bf | |||
| a80bb4b6f3 | |||
| 9b9d210109 | |||
| 817b703d4f | |||
| 88a25453e6 | |||
| 6e3eb1189a | |||
| aea50926a9 | |||
| 299c1b8e7d | |||
| 3f6ab79894 | |||
| b14fee1685 | |||
| ace8fb685d | |||
| 1870ee5e45 | |||
| 8be65fa685 | |||
| cf4e76b12d | |||
| c156463f10 | |||
| e7796550c6 | |||
| c3c9181400 | |||
| 4f4d508685 | |||
| f36a1cdb27 | |||
| 28941f2395 | |||
| 7f02272304 | |||
| 02d86e7b0a | |||
| 4c8b6add98 | |||
| 60d646c392 | |||
| 5e871bc501 | |||
| 549db2fcfe | |||
| d9840d60ef | |||
| 124cf3f66c | |||
| cd78b8de59 | |||
| a3209de000 | |||
| 2908be1705 | |||
| 009a01ebf4 | |||
| 3fdbf3bfef | |||
| c8ecf3cc69 | |||
| 088891c5ca | |||
| 099aa749e1 | |||
| db7aac2b90 | |||
| 366bcf8efc | |||
| a4f1ae351f | |||
| a63d8598d8 | |||
| d7385654ef | |||
| 7399b9f5ca | |||
| 795bfab1aa | |||
| fb5906afe0 | |||
| e38cd0f3ca | |||
| 481112cfc2 | |||
| 3d9730d956 | |||
| 60646edd6b | |||
| 9bd5a99c81 | |||
| d22c5775c2 | |||
| 981e149ca5 | |||
| b83b1a5b21 | |||
| 09e6ace142 | |||
| e83bb087ea | |||
| feaac83ee7 | |||
| 915056203d | |||
| 231276e39b |
@@ -1,2 +1,3 @@
|
||||
/source/node_modules
|
||||
/demo/bin-debug
|
||||
/demo/bin-release
|
||||
|
||||
@@ -1,23 +1,14 @@
|
||||
# egret-framework
|
||||
用于egret的一套框架 包含众多游戏中可能用到的系统
|
||||
|
||||
## 当前版本功能
|
||||
|
||||
[](https://lgtm.com/projects/g/esengine/egret-framework/context:javascript)
|
||||
|
||||
这是一套用于egret的游戏框架,里面包含ECS框架用于管理场景实体,一些常用2D碰撞检测及A*寻路。如果您还需要包含其他的AI系统可以查看作者其他库(行为树、简易FSM、实用AI)。
|
||||
|
||||
## 版本计划功能
|
||||
|
||||
- [x] 简易ECS框架
|
||||
- [x] A*寻路(AStar)
|
||||
- [x] 常用碰撞检测
|
||||
- [x] 数学库
|
||||
- [x] 简易矩阵类
|
||||
- [x] 简易2d 向量类
|
||||
- [x] 掩码实用类
|
||||
- [x] BreadthFirst 寻路算法
|
||||
- [x] Dijkstra 寻路算法
|
||||
- [x] 事件处理器
|
||||
|
||||
## 计划列表
|
||||
|
||||
- [ ] ECS
|
||||
- [ ] 组件列表
|
||||
- [x] 组件列表
|
||||
- [x] 碰撞组件
|
||||
- [x] 移动组件
|
||||
- [ ] 刚体组件
|
||||
@@ -28,16 +19,43 @@
|
||||
- [ ] 网格弹簧组件
|
||||
- [ ] 相机震动组件
|
||||
- [ ] 霓虹灯组件
|
||||
- [ ] 跟随相机组件
|
||||
- [ ] 系统列表
|
||||
- [ ] 被动系统
|
||||
- [ ] 协调系统
|
||||
- [ ] 数学库
|
||||
- [ ] 贝塞尔曲线
|
||||
- [ ] 快速随机数类
|
||||
- [ ] 浮点助手类
|
||||
- [ ] 高性能数组
|
||||
- [x] 跟随相机组件
|
||||
- [x] 系统列表
|
||||
- [x] 被动系统
|
||||
- [x] 协调系统
|
||||
- [x] A*寻路(AStar)
|
||||
- [x] 常用碰撞检测
|
||||
- [x] 数学库
|
||||
- [x] 简易矩阵类
|
||||
- [x] 简易2d 向量类
|
||||
- [x] 掩码实用类
|
||||
- [x] 贝塞尔曲线
|
||||
- [x] 快速随机数类
|
||||
- [x] BreadthFirst 寻路算法
|
||||
- [x] Dijkstra 寻路算法
|
||||
- [x] 事件处理器
|
||||
|
||||
## 关于egret用ecs框架(typescript/javascript)
|
||||
ecs 是功能强大的实体组件系统。typescript与其他语言不同,因此我对ecs的设计尽可能的支持typescript特性。虽然ecs拥有标准实体组件系统,但在细节上有很大不同。创建标准ecs通常处于原始速度、缓存位置和其他性能原因。使用typescript,我们没有struct,因为没有必要匹配标准实体组件系统的设计方式,因为我们对内存布局没有那种控制。
|
||||
|
||||
ecs更灵活,可以更好的集中、组织、排序和过滤游戏中的对象。ecs让您拥有轻量级实体和组件,这些组件可以由系统批量处理。
|
||||
例如,您在制作一个射手,您可能会有几十到几百个子弹,这些作为轻量级实体由系统批量处理。
|
||||
|
||||
所以ecs在设计当中拥有四种重要类型:世界(Scene),过滤器(Matcher),系统(System)和实体(Entity)
|
||||
|
||||
## 世界(Scene)
|
||||
Scene是ecs包含系统和实体最外面的容器。
|
||||
|
||||
## 实体(Entity)
|
||||
实体只由系统处理。
|
||||
|
||||
## 组件(Component)
|
||||
组件应该只包含数据而没有逻辑代码。对数据进行逻辑是系统的工作。
|
||||
|
||||
## 系统(System)
|
||||
ecs中的系统会不断的更新实体。系统使用过滤器选择某些实体,然后仅更新那些选择的实体。
|
||||
|
||||
## 作者其他库(egret)
|
||||
|
||||
- [x] [行为树/实用AI 系统](https://github.com/esengine/egret-BehaviourTree-ai)
|
||||
- [行为树/实用AI 系统](https://github.com/esengine/egret-BehaviourTree-ai)
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported Versions
|
||||
|
||||
Use this section to tell people about which versions of your project are
|
||||
currently being supported with security updates.
|
||||
|
||||
| Version | Supported |
|
||||
| ------- | ------------------ |
|
||||
| 5.1.x | :white_check_mark: |
|
||||
| 5.0.x | :x: |
|
||||
| 4.0.x | :white_check_mark: |
|
||||
| < 4.0 | :x: |
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
Use this section to tell people how to report a vulnerability.
|
||||
|
||||
Tell them where to go, how often they can expect to get an update on a
|
||||
reported vulnerability, what to expect if the vulnerability is accepted or
|
||||
declined, etc.
|
||||
@@ -0,0 +1 @@
|
||||
theme: jekyll-theme-slate
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"version": "0.1.0",
|
||||
"command": "egret",
|
||||
"isShellCommand": true,
|
||||
"showOutput": "silent",
|
||||
"args": [
|
||||
"build"
|
||||
],
|
||||
"problemMatcher": "$tsc"
|
||||
}
|
||||
@@ -27,7 +27,7 @@
|
||||
data-entry-class="Main"
|
||||
data-orientation="auto"
|
||||
data-scale-mode="fixedWidth"
|
||||
data-frame-rate="30"
|
||||
data-frame-rate="60"
|
||||
data-content-width="640"
|
||||
data-content-height="1136"
|
||||
data-multi-fingered="2"
|
||||
@@ -77,7 +77,7 @@
|
||||
* "calculateCanvasScaleFactor": //a function return canvas scale factor
|
||||
* }
|
||||
**/
|
||||
egret.runEgret({ renderMode: "webgl", audioType: 0, calculateCanvasScaleFactor:function(context) {
|
||||
egret.runEgret({ renderMode: "canvas", audioType: 0, calculateCanvasScaleFactor:function(context) {
|
||||
var backingStore = context.backingStorePixelRatio ||
|
||||
context.webkitBackingStorePixelRatio ||
|
||||
context.mozBackingStorePixelRatio ||
|
||||
|
||||
@@ -11,13 +11,15 @@
|
||||
"libs/long/long.js"
|
||||
],
|
||||
"game": [
|
||||
"bin-debug/game/CoreEmitterType.js",
|
||||
"bin-debug/AssetAdapter.js",
|
||||
"bin-debug/LoadingUI.js",
|
||||
"bin-debug/Main.js",
|
||||
"bin-debug/Platform.js",
|
||||
"bin-debug/ThemeAdapter.js",
|
||||
"bin-debug/game/CoreEmitterType.js",
|
||||
"bin-debug/LoadingUI.js",
|
||||
"bin-debug/game/MainScene.js",
|
||||
"bin-debug/game/PlayerController.js",
|
||||
"bin-debug/game/SimplePooled.js",
|
||||
"bin-debug/game/SpawnerComponent.js",
|
||||
"bin-debug/game/SpawnerSystem.js"
|
||||
]
|
||||
|
||||
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 456 B |
|
Before Width: | Height: | Size: 1019 B After Width: | Height: | Size: 438 B |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 439 B |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 535 B |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 538 B |
|
Before Width: | Height: | Size: 1002 B After Width: | Height: | Size: 337 B |
|
Before Width: | Height: | Size: 938 B After Width: | Height: | Size: 314 B |
|
Before Width: | Height: | Size: 1.0 KiB After Width: | Height: | Size: 340 B |
|
Before Width: | Height: | Size: 938 B After Width: | Height: | Size: 314 B |
|
Before Width: | Height: | Size: 976 B After Width: | Height: | Size: 361 B |
|
Before Width: | Height: | Size: 970 B After Width: | Height: | Size: 349 B |
|
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 813 B |
|
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 813 B |
|
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 569 B |
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 458 B |
|
Before Width: | Height: | Size: 933 B After Width: | Height: | Size: 313 B |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 502 B |
|
Before Width: | Height: | Size: 933 B After Width: | Height: | Size: 303 B |
|
Before Width: | Height: | Size: 933 B After Width: | Height: | Size: 313 B |
|
Before Width: | Height: | Size: 1.1 KiB After Width: | Height: | Size: 444 B |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 535 B |
|
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 536 B |
|
Before Width: | Height: | Size: 136 KiB After Width: | Height: | Size: 136 KiB |
|
Before Width: | Height: | Size: 6.6 KiB After Width: | Height: | Size: 4.2 KiB |
@@ -132,9 +132,19 @@
|
||||
"name": "egret_icon_png"
|
||||
},
|
||||
{
|
||||
"name": "description_json",
|
||||
"url": "config/description.json",
|
||||
"type": "json",
|
||||
"url": "config/description.json"
|
||||
"name": "description_json"
|
||||
},
|
||||
{
|
||||
"url": "assets/isometric_grass_and_water.json",
|
||||
"type": "json",
|
||||
"name": "isometric_grass_and_water_json"
|
||||
},
|
||||
{
|
||||
"url": "assets/isometric_grass_and_water.png",
|
||||
"type": "image",
|
||||
"name": "isometric_grass_and_water_png"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -30,6 +30,7 @@
|
||||
|
||||
class Main extends eui.UILayer {
|
||||
public static emitter: Emitter<CoreEmitterType>;
|
||||
public static manager: SceneManager;
|
||||
|
||||
protected createChildren(): void {
|
||||
super.createChildren();
|
||||
@@ -52,6 +53,7 @@ class Main extends eui.UILayer {
|
||||
egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
|
||||
egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());
|
||||
|
||||
Main.manager = new SceneManager(this.stage);
|
||||
Main.emitter = new Emitter<CoreEmitterType>();
|
||||
this.addEventListener(egret.Event.ENTER_FRAME, this.updateFrame, this);
|
||||
this.runGame();
|
||||
@@ -59,14 +61,6 @@ class Main extends eui.UILayer {
|
||||
|
||||
private updateFrame(evt: egret.Event){
|
||||
Main.emitter.emit(CoreEmitterType.Update, evt);
|
||||
let activeScene = SceneManager.getActiveScene();
|
||||
if (activeScene){
|
||||
let player = activeScene.findEntity("player");
|
||||
if (player){
|
||||
let mover = player.getComponent<Mover>(Mover);
|
||||
mover.move(new Vector2(0, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async runGame() {
|
||||
@@ -105,19 +99,7 @@ class Main extends eui.UILayer {
|
||||
* Create scene interface
|
||||
*/
|
||||
protected createGameScene(): void {
|
||||
let scene = SceneManager.createScene("main", new MainScene(this)).setActive();
|
||||
let player = scene.createEntity("player");
|
||||
player.addComponent(new PolygonMesh([new Vector2(0, 0),
|
||||
new Vector2(10, 0),
|
||||
new Vector2(10, 10),
|
||||
new Vector2(0, 10),
|
||||
new Vector2(0, 0)]));
|
||||
player.addComponent(new SpawnComponent(EnemyType.worm));
|
||||
player.addComponent(new BoxCollider()).setSize(100, 100).isTrigger = true;
|
||||
player.addComponent(new Mover());
|
||||
|
||||
let player2 = scene.createEntity("player2");
|
||||
player2.addComponent(new BoxCollider()).setSize(99, 99);
|
||||
SceneManager.scene = new MainScene();
|
||||
|
||||
// Main.emitter.addObserver(CoreEmitterType.Update, ()=>{
|
||||
// console.log("update emitter");
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
class MainScene extends Scene {
|
||||
constructor(displayContent: egret.DisplayObject){
|
||||
super(displayContent);
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
// this.addEntityProcessor(new SpawnerSystem(new Matcher()));
|
||||
this.astarTest();
|
||||
@@ -8,7 +8,53 @@ class MainScene extends Scene {
|
||||
this.breadthfirstTest();
|
||||
}
|
||||
|
||||
public breadthfirstTest(){
|
||||
public async onStart() {
|
||||
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
|
||||
let bg = this.createEntity("bg");
|
||||
bg.addComponent(new SpriteRenderer()).setSprite(sprite).setColor(0xff0000);
|
||||
bg.addComponent(new PlayerController());
|
||||
bg.addComponent(new Mover());
|
||||
bg.addComponent(new ScrollingSpriteRenderer(sprite));
|
||||
bg.addComponent(new BoxCollider());
|
||||
bg.position = new Vector2(Math.random() * 200, Math.random() * 200);
|
||||
|
||||
for (let i = 0; i < 20; i++) {
|
||||
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
|
||||
let player2 = this.createEntity("player2");
|
||||
player2.addComponent(new SpriteRenderer()).setSprite(sprite);
|
||||
player2.position = new Vector2(Math.random() * 1000, Math.random() * 1000);
|
||||
player2.addComponent(new BoxCollider());
|
||||
}
|
||||
|
||||
this.camera.follow(bg, CameraStyle.lockOn);
|
||||
|
||||
let pool = new ComponentPool<SimplePooled>(SimplePooled);
|
||||
let c1 = pool.obtain();
|
||||
let c2 = pool.obtain();
|
||||
pool.free(c1);
|
||||
let c1b = pool.obtain();
|
||||
|
||||
console.log(c1 != c2);
|
||||
console.log(c1 == c1b);
|
||||
|
||||
let button = new eui.Button();
|
||||
button.label = "切换场景";
|
||||
this.addChild(button);
|
||||
button.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
|
||||
SceneManager.startSceneTransition(new FadeTransition(() => {
|
||||
return new MainScene();
|
||||
}));
|
||||
}, this);
|
||||
|
||||
Main.emitter.addObserver(CoreEmitterType.Update, this.handleFuncTest, this);
|
||||
}
|
||||
|
||||
/** 测试Emitter */
|
||||
private handleFuncTest(){
|
||||
Main.emitter.removeObserver(CoreEmitterType.Update, this.handleFuncTest);
|
||||
}
|
||||
|
||||
public breadthfirstTest() {
|
||||
let graph = new UnweightedGraph<string>();
|
||||
|
||||
graph.addEdgesForNode("a", ["b"]); // a->b
|
||||
@@ -22,27 +68,28 @@ class MainScene extends Scene {
|
||||
console.log(path);
|
||||
}
|
||||
|
||||
public dijkstraTest(){
|
||||
public dijkstraTest() {
|
||||
let graph = new WeightedGridGraph(20, 20);
|
||||
|
||||
graph.weightedNodes.push(new Point(3, 3));
|
||||
graph.weightedNodes.push(new Point(3, 4));
|
||||
graph.weightedNodes.push(new Point(4, 3));
|
||||
graph.weightedNodes.push(new Point(4, 4));
|
||||
graph.weightedNodes.push(new Vector2(3, 3));
|
||||
graph.weightedNodes.push(new Vector2(3, 4));
|
||||
graph.weightedNodes.push(new Vector2(4, 3));
|
||||
graph.weightedNodes.push(new Vector2(4, 4));
|
||||
|
||||
let path = graph.search(new Point(3, 4), new Point(15, 17));
|
||||
let path = graph.search(new Vector2(3, 4), new Vector2(15, 17));
|
||||
console.log(path);
|
||||
}
|
||||
|
||||
public astarTest(){
|
||||
let graph = new AstarGridGraph(20, 20);
|
||||
public astarTest() {
|
||||
let graph = new AstarGridGraph(30, 30);
|
||||
|
||||
graph.weightedNodes.push(new Point(3, 3));
|
||||
graph.weightedNodes.push(new Point(3, 4));
|
||||
graph.weightedNodes.push(new Point(4, 3));
|
||||
graph.weightedNodes.push(new Point(4, 4));
|
||||
// graph.weightedNodes.push(new Vector2(3, 3));
|
||||
// graph.weightedNodes.push(new Vector2(3, 4));
|
||||
// graph.weightedNodes.push(new Vector2(4, 3));
|
||||
// graph.weightedNodes.push(new Vector2(4, 4));
|
||||
|
||||
let path = graph.search(new Point(3, 4), new Point(15, 17));
|
||||
console.log(path);
|
||||
let startTime = egret.getTimer();
|
||||
let path = graph.search(new Vector2(1, 1), new Vector2(29, 29));
|
||||
console.log(egret.getTimer() - startTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
class PlayerController extends Component {
|
||||
private down: boolean = false;
|
||||
private touchPoint: Vector2 = Vector2.zero;
|
||||
private mover: Mover;
|
||||
private spriteRenderer: SpriteRenderer;
|
||||
|
||||
public onAddedToEntity(){
|
||||
this.entity.scene.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchBegin, this);
|
||||
this.entity.scene.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchBegin, this);
|
||||
this.entity.scene.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.touchEnd, this);
|
||||
}
|
||||
|
||||
private touchBegin(evt: egret.TouchEvent){
|
||||
this.down = true;
|
||||
this.touchPoint = new Vector2(evt.stageX, evt.stageY);
|
||||
}
|
||||
|
||||
private touchEnd(evt: egret.TouchEvent){
|
||||
this.down = false;
|
||||
this.touchPoint = new Vector2(evt.stageX, evt.stageY);
|
||||
}
|
||||
|
||||
public update(){
|
||||
if (!this.mover)
|
||||
this.mover = this.entity.getComponent<Mover>(Mover);
|
||||
|
||||
if (!this.spriteRenderer)
|
||||
this.spriteRenderer = this.entity.getComponent<SpriteRenderer>(SpriteRenderer);
|
||||
|
||||
if (!this.mover)
|
||||
return;
|
||||
|
||||
if (!SpriteRenderer)
|
||||
return;
|
||||
|
||||
if (this.down){
|
||||
let camera = SceneManager.scene.camera;
|
||||
let moveLeft: number = 0;
|
||||
let moveRight: number = 0;
|
||||
let speed = 100;
|
||||
let worldPos = Input.touchPosition;
|
||||
if (worldPos.x < this.spriteRenderer.localPosition.x){
|
||||
moveLeft = -1;
|
||||
} else if(worldPos.x > this.spriteRenderer.localPosition.x){
|
||||
moveLeft = 1;
|
||||
}
|
||||
|
||||
if (worldPos.y < this.spriteRenderer.localPosition.y){
|
||||
moveRight = -1;
|
||||
} else if(worldPos.y > this.spriteRenderer.localPosition.y){
|
||||
moveRight = 1;
|
||||
}
|
||||
this.mover.move(new Vector2(moveLeft * speed * Time.deltaTime, moveRight * speed * Time.deltaTime));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
class SimplePooled extends PooledComponent {
|
||||
public reset(){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ egret_native.egretStart = function () {
|
||||
//The following is automatically modified, please do not modify
|
||||
//----auto option start----
|
||||
entryClassName: "Main",
|
||||
frameRate: 30,
|
||||
frameRate: 60,
|
||||
scaleMode: "fixedWidth",
|
||||
contentWidth: 640,
|
||||
contentHeight: 1136,
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"typescript.tsdk": "./node_modules/typescript/lib"
|
||||
}
|
||||
@@ -10967,13 +10967,13 @@ declare namespace egret {
|
||||
const RUNTIME2 = "runtime2";
|
||||
/**
|
||||
* Running on Alipay
|
||||
* @version Egret 5.2.23
|
||||
* @version Egret 5.2.33
|
||||
* @platform All
|
||||
* @language en_US
|
||||
*/
|
||||
/**
|
||||
* 运行在支付宝小游戏上
|
||||
* @version Egret 5.2.26
|
||||
* @version Egret 5.2.33
|
||||
* @platform All
|
||||
* @language zh_CN
|
||||
*/
|
||||
|
||||
@@ -3,6 +3,12 @@
|
||||
* 计算路径给定的IAstarGraph和开始/目标位置
|
||||
*/
|
||||
class AStarPathfinder {
|
||||
/**
|
||||
* 尽可能从开始到目标找到一条路径。如果没有找到路径,则返回null。
|
||||
* @param graph
|
||||
* @param start
|
||||
* @param goal
|
||||
*/
|
||||
public static search<T>(graph: IAstarGraph<T>, start: T, goal: T){
|
||||
let foundPath = false;
|
||||
let cameFrom = new Map<T, T>();
|
||||
@@ -60,6 +66,12 @@ class AStarPathfinder {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从cameFrom字典重新构造路径
|
||||
* @param cameFrom
|
||||
* @param start
|
||||
* @param goal
|
||||
*/
|
||||
public static recontructPath<T>(cameFrom: Map<T, T>, start: T, goal: T): T[]{
|
||||
let path = [];
|
||||
let current = goal;
|
||||
|
||||
@@ -2,22 +2,22 @@
|
||||
* 基本静态网格图与A*一起使用
|
||||
* 将walls添加到walls HashSet,并将加权节点添加到weightedNodes
|
||||
*/
|
||||
class AstarGridGraph implements IAstarGraph<Point> {
|
||||
public dirs: Point[] = [
|
||||
new Point(1, 0),
|
||||
new Point(0, -1),
|
||||
new Point(-1, 0),
|
||||
new Point(0, 1)
|
||||
class AstarGridGraph implements IAstarGraph<Vector2> {
|
||||
public dirs: Vector2[] = [
|
||||
new Vector2(1, 0),
|
||||
new Vector2(0, -1),
|
||||
new Vector2(-1, 0),
|
||||
new Vector2(0, 1)
|
||||
];
|
||||
|
||||
public walls: Point[] = [];
|
||||
public weightedNodes: Point[] = [];
|
||||
public walls: Vector2[] = [];
|
||||
public weightedNodes: Vector2[] = [];
|
||||
public defaultWeight: number = 1;
|
||||
public weightedNodeWeight = 5;
|
||||
|
||||
private _width;
|
||||
private _height;
|
||||
private _neighbors: Point[] = new Array(4);
|
||||
private _neighbors: Vector2[] = new Array(4);
|
||||
|
||||
constructor(width: number, height: number){
|
||||
this._width = width;
|
||||
@@ -28,27 +28,32 @@ class AstarGridGraph implements IAstarGraph<Point> {
|
||||
* 确保节点在网格图的边界内
|
||||
* @param node
|
||||
*/
|
||||
public isNodeInBounds(node: Point): boolean {
|
||||
public isNodeInBounds(node: Vector2): boolean {
|
||||
return 0 <= node.x && node.x < this._width && 0 <= node.y && node.y < this._height;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查节点是否可以通过。墙壁是不可逾越的。
|
||||
* 检查节点是否可以通过。walls是不可逾越的。
|
||||
* @param node
|
||||
*/
|
||||
public isNodePassable(node: Point): boolean {
|
||||
public isNodePassable(node: Vector2): boolean {
|
||||
return !this.walls.firstOrDefault(wall => JSON.stringify(wall) == JSON.stringify(node));
|
||||
}
|
||||
|
||||
public search(start: Point, goal: Point){
|
||||
/**
|
||||
* 调用AStarPathfinder.search的快捷方式
|
||||
* @param start
|
||||
* @param goal
|
||||
*/
|
||||
public search(start: Vector2, goal: Vector2){
|
||||
return AStarPathfinder.search(this, start, goal);
|
||||
}
|
||||
|
||||
public getNeighbors(node: Point): Point[] {
|
||||
public getNeighbors(node: Vector2): Vector2[] {
|
||||
this._neighbors.length = 0;
|
||||
|
||||
this.dirs.forEach(dir => {
|
||||
let next = new Point(node.x + dir.x, node.y + dir.y);
|
||||
let next = new Vector2(node.x + dir.x, node.y + dir.y);
|
||||
if (this.isNodeInBounds(next) && this.isNodePassable(next))
|
||||
this._neighbors.push(next);
|
||||
});
|
||||
@@ -56,11 +61,11 @@ class AstarGridGraph implements IAstarGraph<Point> {
|
||||
return this._neighbors;
|
||||
}
|
||||
|
||||
public cost(from: Point, to: Point): number {
|
||||
public cost(from: Vector2, to: Vector2): number {
|
||||
return this.weightedNodes.find((p)=> JSON.stringify(p) == JSON.stringify(to)) ? this.weightedNodeWeight : this.defaultWeight;
|
||||
}
|
||||
|
||||
public heuristic(node: Point, goal: Point) {
|
||||
public heuristic(node: Vector2, goal: Vector2) {
|
||||
return Math.abs(node.x - goal.x) + Math.abs(node.y - goal.y);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,22 @@
|
||||
/**
|
||||
* graph的接口,可以提供给AstarPathfinder.search方法
|
||||
*/
|
||||
interface IAstarGraph<T> {
|
||||
/**
|
||||
* getNeighbors方法应该返回从传入的节点可以到达的任何相邻节点
|
||||
* @param node
|
||||
*/
|
||||
getNeighbors(node: T): Array<T>;
|
||||
/**
|
||||
* 计算从从from到to的成本
|
||||
* @param from
|
||||
* @param to
|
||||
*/
|
||||
cost(from: T, to: T): number;
|
||||
/**
|
||||
* 计算从node到to的启发式。参见WeightedGridGraph了解常用的Manhatten方法。
|
||||
* @param node
|
||||
* @param goal
|
||||
*/
|
||||
heuristic(node: T, goal: T);
|
||||
}
|
||||
@@ -1,27 +1,74 @@
|
||||
/**
|
||||
* 使用堆实现最小优先级队列 O(1)复杂度
|
||||
* 这种查找速度比使用字典快5-10倍
|
||||
* 但是,由于IPriorityQueue.contains()是许多寻路算法中调用最多的方法,因此尽可能快地实现它对于我们的应用程序非常重要。
|
||||
*/
|
||||
class PriorityQueue<T extends PriorityQueueNode> {
|
||||
private _numNodes: number;
|
||||
private _nodes: T[];
|
||||
private _numNodesEverEnqueued;
|
||||
|
||||
/**
|
||||
* 实例化一个新的优先级队列
|
||||
* @param maxNodes 允许加入队列的最大节点(执行此操作将导致undefined的行为)
|
||||
*/
|
||||
constructor(maxNodes: number) {
|
||||
this._numNodes = 0;
|
||||
this._nodes = new Array(maxNodes + 1);
|
||||
this._numNodesEverEnqueued = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从队列中删除每个节点。
|
||||
* O(n)复杂度 所有尽可能少调用该方法
|
||||
*/
|
||||
public clear() {
|
||||
this._nodes.splice(1, this._numNodes);
|
||||
this._numNodes = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回队列中的节点数。
|
||||
* O(1)复杂度
|
||||
*/
|
||||
public get count() {
|
||||
return this._numNodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回可同时进入此队列的最大项数。一旦你达到这个数字(即。一旦Count == MaxSize),尝试加入另一个项目将导致undefined的行为
|
||||
* O(1)复杂度
|
||||
*/
|
||||
public get maxSize() {
|
||||
return this._nodes.length - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回(在O(1)中)给定节点是否在队列中
|
||||
* O (1)复杂度
|
||||
* @param node
|
||||
*/
|
||||
public contains(node: T): boolean {
|
||||
if (!node){
|
||||
console.error("node cannot be null");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (node.queueIndex < 0 || node.queueIndex >= this._nodes.length){
|
||||
console.error("node.QueueIndex has been corrupted. Did you change it manually? Or add this node to another queue?");
|
||||
return false;
|
||||
}
|
||||
|
||||
return (this._nodes[node.queueIndex] == node);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将节点放入优先队列 较低的值放在前面 先入先出
|
||||
* 如果队列已满,则结果undefined。如果节点已经加入队列,则结果undefined。
|
||||
* O(log n)
|
||||
* @param node
|
||||
* @param priority
|
||||
*/
|
||||
public enqueue(node: T, priority: number) {
|
||||
node.priority = priority;
|
||||
this._numNodes++;
|
||||
@@ -31,12 +78,21 @@ class PriorityQueue<T extends PriorityQueueNode> {
|
||||
this.cascadeUp(this._nodes[this._numNodes]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除队列头(具有最小优先级的节点;按插入顺序断开连接),并返回它。如果队列为空,结果undefined
|
||||
* O(log n)
|
||||
*/
|
||||
public dequeue(): T {
|
||||
let returnMe = this._nodes[1];
|
||||
this.remove(returnMe);
|
||||
return returnMe;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从队列中删除一个节点。节点不需要是队列的头。如果节点不在队列中,则结果未定义。如果不确定,首先检查Contains()
|
||||
* O(log n)
|
||||
* @param node
|
||||
*/
|
||||
public remove(node: T) {
|
||||
if (node.queueIndex == this._numNodes) {
|
||||
this._nodes[this._numNodes] = null;
|
||||
@@ -52,6 +108,9 @@ class PriorityQueue<T extends PriorityQueueNode> {
|
||||
this.onNodeUpdated(formerLastNode);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查以确保队列仍然处于有效状态。用于测试/调试队列。
|
||||
*/
|
||||
public isValidQueue(): boolean {
|
||||
for (let i = 1; i < this._nodes.length; i++) {
|
||||
if (this._nodes[i]) {
|
||||
@@ -71,24 +130,29 @@ class PriorityQueue<T extends PriorityQueueNode> {
|
||||
}
|
||||
|
||||
private onNodeUpdated(node: T) {
|
||||
// 将更新后的节点按适当的方式向上或向下冒泡
|
||||
let parentIndex = Math.floor(node.queueIndex / 2);
|
||||
let parentNode = this._nodes[parentIndex];
|
||||
|
||||
if (parentIndex > 0 && this.hasHigherPriority(node, parentNode)) {
|
||||
this.cascadeUp(node);
|
||||
} else {
|
||||
// 注意,如果parentNode == node(即节点是根),则将调用CascadeDown。
|
||||
this.cascadeDown(node);
|
||||
}
|
||||
}
|
||||
|
||||
private cascadeDown(node: T) {
|
||||
// 又名Heapify-down
|
||||
let newParent: T;
|
||||
let finalQueueIndex = node.queueIndex;
|
||||
while (true) {
|
||||
newParent = node;
|
||||
let childLeftIndex = 2 * finalQueueIndex;
|
||||
|
||||
// 检查左子节点的优先级是否高于当前节点
|
||||
if (childLeftIndex > this._numNodes) {
|
||||
// 这可以放在循环之外,但是我们必须检查newParent != node两次
|
||||
node.queueIndex = finalQueueIndex;
|
||||
this._nodes[finalQueueIndex] = node;
|
||||
break;
|
||||
@@ -99,6 +163,7 @@ class PriorityQueue<T extends PriorityQueueNode> {
|
||||
newParent = childLeft;
|
||||
}
|
||||
|
||||
// 检查右子节点的优先级是否高于当前节点或左子节点
|
||||
let childRightIndex = childLeftIndex + 1;
|
||||
if (childRightIndex <= this._numNodes) {
|
||||
let childRight = this._nodes[childRightIndex];
|
||||
@@ -107,13 +172,17 @@ class PriorityQueue<T extends PriorityQueueNode> {
|
||||
}
|
||||
}
|
||||
|
||||
// 如果其中一个子节点具有更高(更小)的优先级,则交换并继续级联
|
||||
if (newParent != node) {
|
||||
// 将新的父节点移动到它的新索引
|
||||
// 节点将被移动一次,这样做比调用Swap()少一个赋值操作。
|
||||
this._nodes[finalQueueIndex] = newParent;
|
||||
|
||||
let temp = newParent.queueIndex;
|
||||
newParent.queueIndex = finalQueueIndex;
|
||||
finalQueueIndex = temp;
|
||||
} else {
|
||||
// 参见上面的笔记
|
||||
node.queueIndex = finalQueueIndex;
|
||||
this._nodes[finalQueueIndex] = node;
|
||||
break;
|
||||
@@ -121,13 +190,20 @@ class PriorityQueue<T extends PriorityQueueNode> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当没有内联时,性能会稍微好一些
|
||||
* @param node
|
||||
*/
|
||||
private cascadeUp(node: T) {
|
||||
// 又名Heapify-up
|
||||
let parent = Math.floor(node.queueIndex / 2);
|
||||
while (parent >= 1) {
|
||||
let parentNode = this._nodes[parent];
|
||||
if (this.hasHigherPriority(parentNode, node))
|
||||
break;
|
||||
|
||||
// 节点具有较低的优先级值,因此将其向上移动到堆中
|
||||
// 出于某种原因,使用Swap()比使用单独的操作更快,如CascadeDown()
|
||||
this.swap(node, parentNode);
|
||||
|
||||
parent = Math.floor(node.queueIndex / 2);
|
||||
@@ -135,14 +211,22 @@ class PriorityQueue<T extends PriorityQueueNode> {
|
||||
}
|
||||
|
||||
private swap(node1: T, node2: T) {
|
||||
// 交换节点
|
||||
this._nodes[node1.queueIndex] = node2;
|
||||
this._nodes[node2.queueIndex] = node1;
|
||||
|
||||
// 交换他们的indicies
|
||||
let temp = node1.queueIndex;
|
||||
node1.queueIndex = node2.queueIndex;
|
||||
node2.queueIndex = temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果higher的优先级高于lower,则返回true,否则返回false。
|
||||
* 注意,调用HasHigherPriority(节点,节点)(即。两个参数为同一个节点)将返回false
|
||||
* @param higher
|
||||
* @param lower
|
||||
*/
|
||||
private hasHigherPriority(higher: T, lower: T) {
|
||||
return (higher.priority < lower.priority ||
|
||||
(higher.priority == lower.priority && higher.insertionIndex < lower.insertionIndex));
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
///<reference path="../../../Math/Point.ts" />
|
||||
///<reference path="../../../Math/Vector2.ts" />
|
||||
/**
|
||||
* 基本的未加权网格图形用于BreadthFirstPathfinder
|
||||
*/
|
||||
class UnweightedGridGraph implements IUnweightedGraph<Point> {
|
||||
private static readonly CARDINAL_DIRS: Point[] = [
|
||||
new Point(1, 0),
|
||||
new Point(0, -1),
|
||||
new Point(-1, 0),
|
||||
new Point(0, -1)
|
||||
class UnweightedGridGraph implements IUnweightedGraph<Vector2> {
|
||||
private static readonly CARDINAL_DIRS: Vector2[] = [
|
||||
new Vector2(1, 0),
|
||||
new Vector2(0, -1),
|
||||
new Vector2(-1, 0),
|
||||
new Vector2(0, -1)
|
||||
];
|
||||
|
||||
private static readonly COMPASS_DIRS = [
|
||||
new Point(1, 0),
|
||||
new Point(1, -1),
|
||||
new Point(0, -1),
|
||||
new Point(-1, -1),
|
||||
new Point(-1, 0),
|
||||
new Point(-1, 1),
|
||||
new Point(0, 1),
|
||||
new Point(1, 1),
|
||||
new Vector2(1, 0),
|
||||
new Vector2(1, -1),
|
||||
new Vector2(0, -1),
|
||||
new Vector2(-1, -1),
|
||||
new Vector2(-1, 0),
|
||||
new Vector2(-1, 1),
|
||||
new Vector2(0, 1),
|
||||
new Vector2(1, 1),
|
||||
];
|
||||
|
||||
public walls: Point[] = [];
|
||||
public walls: Vector2[] = [];
|
||||
|
||||
private _width: number;
|
||||
private _hegiht: number;
|
||||
|
||||
private _dirs: Point[];
|
||||
private _neighbors: Point[] = new Array(4);
|
||||
private _dirs: Vector2[];
|
||||
private _neighbors: Vector2[] = new Array(4);
|
||||
|
||||
constructor(width: number, height: number, allowDiagonalSearch: boolean = false) {
|
||||
this._width = width;
|
||||
@@ -35,19 +35,19 @@ class UnweightedGridGraph implements IUnweightedGraph<Point> {
|
||||
this._dirs = allowDiagonalSearch ? UnweightedGridGraph.COMPASS_DIRS : UnweightedGridGraph.CARDINAL_DIRS;
|
||||
}
|
||||
|
||||
public isNodeInBounds(node: Point): boolean {
|
||||
public isNodeInBounds(node: Vector2): boolean {
|
||||
return 0 <= node.x && node.x < this._width && 0 <= node.y && node.y < this._hegiht;
|
||||
}
|
||||
|
||||
public isNodePassable(node: Point): boolean {
|
||||
public isNodePassable(node: Vector2): boolean {
|
||||
return !this.walls.firstOrDefault(wall => JSON.stringify(wall) == JSON.stringify(node));
|
||||
}
|
||||
|
||||
public getNeighbors(node: Point) {
|
||||
public getNeighbors(node: Vector2) {
|
||||
this._neighbors.length = 0;
|
||||
|
||||
this._dirs.forEach(dir => {
|
||||
let next = new Point(node.x + dir.x, node.y + dir.y);
|
||||
let next = new Vector2(node.x + dir.x, node.y + dir.y);
|
||||
if (this.isNodeInBounds(next) && this.isNodePassable(next))
|
||||
this._neighbors.push(next);
|
||||
});
|
||||
@@ -55,7 +55,7 @@ class UnweightedGridGraph implements IUnweightedGraph<Point> {
|
||||
return this._neighbors;
|
||||
}
|
||||
|
||||
public search(start: Point, goal: Point): Point[] {
|
||||
public search(start: Vector2, goal: Vector2): Vector2[] {
|
||||
return BreadthFirstPathfinder.search(this, start, goal);
|
||||
}
|
||||
}
|
||||
@@ -1,35 +1,35 @@
|
||||
///<reference path="../../../Math/Point.ts" />
|
||||
///<reference path="../../../Math/Vector2.ts" />
|
||||
/**
|
||||
* 支持一种加权节点的基本网格图
|
||||
*/
|
||||
class WeightedGridGraph implements IWeightedGraph<Point> {
|
||||
class WeightedGridGraph implements IWeightedGraph<Vector2> {
|
||||
public static readonly CARDINAL_DIRS = [
|
||||
new Point(1, 0),
|
||||
new Point(0, -1),
|
||||
new Point(-1, 0),
|
||||
new Point(0, 1)
|
||||
new Vector2(1, 0),
|
||||
new Vector2(0, -1),
|
||||
new Vector2(-1, 0),
|
||||
new Vector2(0, 1)
|
||||
];
|
||||
|
||||
private static readonly COMPASS_DIRS = [
|
||||
new Point(1, 0),
|
||||
new Point(1, -1),
|
||||
new Point(0, -1),
|
||||
new Point(-1, -1),
|
||||
new Point(-1, 0),
|
||||
new Point(-1, 1),
|
||||
new Point(0, 1),
|
||||
new Point(1, 1),
|
||||
new Vector2(1, 0),
|
||||
new Vector2(1, -1),
|
||||
new Vector2(0, -1),
|
||||
new Vector2(-1, -1),
|
||||
new Vector2(-1, 0),
|
||||
new Vector2(-1, 1),
|
||||
new Vector2(0, 1),
|
||||
new Vector2(1, 1),
|
||||
];
|
||||
|
||||
public walls: Point[] = [];
|
||||
public weightedNodes: Point[] = [];
|
||||
public walls: Vector2[] = [];
|
||||
public weightedNodes: Vector2[] = [];
|
||||
public defaultWeight = 1;
|
||||
public weightedNodeWeight = 5;
|
||||
|
||||
private _width: number;
|
||||
private _height: number;
|
||||
private _dirs: Point[];
|
||||
private _neighbors: Point[] = new Array(4);
|
||||
private _dirs: Vector2[];
|
||||
private _neighbors: Vector2[] = new Array(4);
|
||||
|
||||
constructor(width: number, height: number, allowDiagonalSearch: boolean = false){
|
||||
this._width = width;
|
||||
@@ -37,23 +37,23 @@ class WeightedGridGraph implements IWeightedGraph<Point> {
|
||||
this._dirs = allowDiagonalSearch ? WeightedGridGraph.COMPASS_DIRS : WeightedGridGraph.CARDINAL_DIRS;
|
||||
}
|
||||
|
||||
public isNodeInBounds(node: Point){
|
||||
public isNodeInBounds(node: Vector2){
|
||||
return 0 <= node.x && node.x < this._width && 0 <= node.y && node.y < this._height;
|
||||
}
|
||||
|
||||
public isNodePassable(node: Point): boolean {
|
||||
public isNodePassable(node: Vector2): boolean {
|
||||
return !this.walls.firstOrDefault(wall => JSON.stringify(wall) == JSON.stringify(node));
|
||||
}
|
||||
|
||||
public search(start: Point, goal: Point){
|
||||
public search(start: Vector2, goal: Vector2){
|
||||
return WeightedPathfinder.search(this, start, goal);
|
||||
}
|
||||
|
||||
public getNeighbors(node: Point): Point[]{
|
||||
public getNeighbors(node: Vector2): Vector2[]{
|
||||
this._neighbors.length = 0;
|
||||
|
||||
this._dirs.forEach(dir => {
|
||||
let next = new Point(node.x + dir.x, node.y + dir.y);
|
||||
let next = new Vector2(node.x + dir.x, node.y + dir.y);
|
||||
if (this.isNodeInBounds(next) && this.isNodePassable(next))
|
||||
this._neighbors.push(next);
|
||||
});
|
||||
@@ -61,7 +61,7 @@ class WeightedGridGraph implements IWeightedGraph<Point> {
|
||||
return this._neighbors;
|
||||
}
|
||||
|
||||
public cost(from: Point, to: Point): number{
|
||||
public cost(from: Vector2, to: Vector2): number{
|
||||
return this.weightedNodes.find(t => JSON.stringify(t) == JSON.stringify(to)) ? this.weightedNodeWeight : this.defaultWeight;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
class Debug {
|
||||
private static _debugDrawItems: DebugDrawItem[] = [];
|
||||
|
||||
public static drawHollowRect(rectanle: Rectangle, color: number, duration = 0){
|
||||
this._debugDrawItems.push(new DebugDrawItem(rectanle, color, duration));
|
||||
}
|
||||
|
||||
public static render(){
|
||||
if (this._debugDrawItems.length > 0){
|
||||
let debugShape = new egret.Shape();
|
||||
if (SceneManager.scene){
|
||||
SceneManager.scene.addChild(debugShape);
|
||||
}
|
||||
|
||||
for (let i = this._debugDrawItems.length - 1; i >= 0; i --){
|
||||
let item = this._debugDrawItems[i];
|
||||
if (item.draw(debugShape))
|
||||
this._debugDrawItems.removeAt(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
enum DebugDrawType {
|
||||
line,
|
||||
hollowRectangle,
|
||||
pixel,
|
||||
text
|
||||
}
|
||||
|
||||
class DebugDrawItem {
|
||||
public rectangle: Rectangle;
|
||||
public color: number;
|
||||
public duration: number;
|
||||
public drawType: DebugDrawType;
|
||||
public text: string;
|
||||
public start: Vector2;
|
||||
public end: Vector2;
|
||||
public x: number;
|
||||
public y: number;
|
||||
public size: number;
|
||||
|
||||
constructor(rectangle: Rectangle, color: number, duration: number){
|
||||
this.rectangle = rectangle;
|
||||
this.color = color;
|
||||
this.duration = duration;
|
||||
this.drawType = DebugDrawType.hollowRectangle;
|
||||
}
|
||||
|
||||
public draw(shape: egret.Shape): boolean{
|
||||
switch (this.drawType){
|
||||
case DebugDrawType.line:
|
||||
DrawUtils.drawLine(shape, this.start, this.end, this.color);
|
||||
break;
|
||||
case DebugDrawType.hollowRectangle:
|
||||
DrawUtils.drawHollowRect(shape, this.rectangle, this.color);
|
||||
break;
|
||||
case DebugDrawType.pixel:
|
||||
DrawUtils.drawPixel(shape, new Vector2(this.x, this.y), this.color, this.size);
|
||||
break;
|
||||
case DebugDrawType.text:
|
||||
break;
|
||||
}
|
||||
|
||||
this.duration -= Time.deltaTime;
|
||||
return this.duration < 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
abstract class Component {
|
||||
abstract class Component extends egret.DisplayObjectContainer {
|
||||
public entity: Entity;
|
||||
private _enabled: boolean = true;
|
||||
public updateInterval: number = 1;
|
||||
|
||||
public get transform(){
|
||||
return this.entity.transform;
|
||||
}
|
||||
/** 允许用户为实体存入信息 */
|
||||
public userData: any;
|
||||
private _updateOrder = 0;
|
||||
|
||||
public get enabled(){
|
||||
return this.entity ? this.entity.enabled && this._enabled : this._enabled;
|
||||
@@ -15,6 +14,10 @@ abstract class Component {
|
||||
this.setEnabled(value);
|
||||
}
|
||||
|
||||
public get localPosition(){
|
||||
return new Vector2(this.entity.x + this.x, this.entity.y + this.y);
|
||||
}
|
||||
|
||||
public setEnabled(isEnabled: boolean){
|
||||
if (this._enabled != isEnabled){
|
||||
this._enabled = isEnabled;
|
||||
@@ -29,8 +32,23 @@ abstract class Component {
|
||||
return this;
|
||||
}
|
||||
|
||||
public initialize(){
|
||||
/** 更新此实体上组件的顺序 */
|
||||
public get updateOrder(){
|
||||
return this._updateOrder;
|
||||
}
|
||||
/** 更新此实体上组件的顺序 */
|
||||
public set updateOrder(value: number){
|
||||
this.setUpdateOrder(value);
|
||||
}
|
||||
public setUpdateOrder(updateOrder: number){
|
||||
if (this._updateOrder != updateOrder){
|
||||
this._updateOrder = updateOrder;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public initialize(){
|
||||
}
|
||||
|
||||
public onAddedToEntity(){
|
||||
@@ -49,16 +67,20 @@ abstract class Component {
|
||||
|
||||
}
|
||||
|
||||
public onEntityTransformChanged(comp: ComponentTransform){
|
||||
public debugRender(){
|
||||
|
||||
}
|
||||
|
||||
public update(){
|
||||
|
||||
|
||||
}
|
||||
|
||||
public debugRender(){
|
||||
|
||||
/**
|
||||
* 当实体的位置改变时调用。这允许组件知道它们由于父实体的移动而移动了。
|
||||
* @param comp
|
||||
*/
|
||||
public onEntityTransformChanged(comp: TransformComponent){
|
||||
|
||||
}
|
||||
|
||||
/** 内部使用 运行时不应该调用 */
|
||||
|
||||
@@ -1,49 +1,30 @@
|
||||
///<reference path="../Component.ts"/>
|
||||
class Camera extends Component {
|
||||
private _zoom;
|
||||
private _origin: Vector2;
|
||||
private _transformMatrix: Matrix2D = Matrix2D.identity;
|
||||
private _inverseTransformMatrix = Matrix2D.identity;
|
||||
private _origin: Vector2 = Vector2.zero;
|
||||
|
||||
private _minimumZoom = 0.3;
|
||||
private _maximumZoom = 3;
|
||||
private _areMatrixesDirty = true;
|
||||
private _inset: CameraInset;
|
||||
private _bounds: Rectangle;
|
||||
private _areBoundsDirty = true;
|
||||
|
||||
public get bounds(){
|
||||
if (this._areMatrixesDirty)
|
||||
this.updateMatrixes();
|
||||
|
||||
if (this._areBoundsDirty){
|
||||
let stage = this.entity.scene.stage;
|
||||
let topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top));
|
||||
let bottomRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, stage.stageHeight - this._inset.bottom));
|
||||
|
||||
if (this.entity.transform.rotation != 0){
|
||||
let topRight = this.screenToWorldPoint(new Vector2(stage.stageWidth - this._inset.right, this._inset.top));
|
||||
let bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left, stage.stageHeight - this._inset.bottom));
|
||||
|
||||
let minX = MathHelper.minOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let maxX = MathHelper.maxOf(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
|
||||
let minY = MathHelper.minOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||
let maxY = MathHelper.maxOf(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
|
||||
|
||||
this._bounds.location = new Vector2(minX, minY);
|
||||
this._bounds.width = maxX - minX;
|
||||
this._bounds.height = maxY - minY;
|
||||
}else{
|
||||
this._bounds.location = topLeft;
|
||||
this._bounds.width = bottomRight.x - topLeft.x;
|
||||
this._bounds.height = bottomRight.y - topLeft.y;
|
||||
}
|
||||
|
||||
this._areBoundsDirty = false;
|
||||
}
|
||||
|
||||
return this._bounds;
|
||||
}
|
||||
private _position: Vector2 = Vector2.zero;
|
||||
/**
|
||||
* 如果相机模式为cameraWindow 则会进行缓动移动
|
||||
* 该值为移动速度
|
||||
*/
|
||||
public followLerp = 0.1;
|
||||
public deadzone: Rectangle = new Rectangle();
|
||||
/** 锁定偏移量 默认中心 */
|
||||
public focusOffset: Vector2 = new Vector2();
|
||||
/** 是否地图锁定 如果锁定则需要设置mapSize属性 */
|
||||
public mapLockEnabled: boolean = false;
|
||||
/** 设置地图大小 默认从0 0左上角开始 只需要输入地图宽高 */
|
||||
public mapSize: Vector2 = new Vector2();
|
||||
/** 跟随的实体 设置后镜头将锁定目标为中心 */
|
||||
public targetEntity: Entity;
|
||||
private _worldSpaceDeadZone: Rectangle = new Rectangle();
|
||||
private _desiredPositionDelta: Vector2 = new Vector2();
|
||||
private _targetCollider: Collider;
|
||||
/** 相机模式 */
|
||||
public cameraStyle: CameraStyle = CameraStyle.lockOn;
|
||||
|
||||
public get zoom(){
|
||||
if (this._zoom == 0)
|
||||
@@ -82,28 +63,45 @@ class Camera extends Component {
|
||||
public set origin(value: Vector2){
|
||||
if (this._origin != value){
|
||||
this._origin = value;
|
||||
this._areMatrixesDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
public get transformMatrix(){
|
||||
if (this._areBoundsDirty)
|
||||
this.updateMatrixes();
|
||||
return this._transformMatrix;
|
||||
public get position(){
|
||||
return this._position;
|
||||
}
|
||||
|
||||
public get inverseTransformMatrix(){
|
||||
if (this._areBoundsDirty)
|
||||
this.updateMatrixes();
|
||||
return this._inverseTransformMatrix;
|
||||
public set position(value: Vector2){
|
||||
this._position = value;
|
||||
}
|
||||
|
||||
public get x(){
|
||||
return this._position.x;
|
||||
}
|
||||
public set x(value: number){
|
||||
this._position.x = value;
|
||||
}
|
||||
public get y(){
|
||||
return this._position.y;
|
||||
}
|
||||
public set y(value: number){
|
||||
this._position.y = value;
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.width = SceneManager.stage.stageWidth;
|
||||
this.height = SceneManager.stage.stageHeight;
|
||||
this.setZoom(0);
|
||||
}
|
||||
|
||||
public onSceneSizeChanged(newWidth: number, newHeight: number){
|
||||
let oldOrigin = this._origin;
|
||||
this.origin = new Vector2(newWidth / 2, newHeight / 2);
|
||||
|
||||
this.entity.position = Vector2.add(this.entity.position, Vector2.subtract(this._origin, oldOrigin));
|
||||
}
|
||||
|
||||
public setMinimumZoom(minZoom: number): Camera{
|
||||
if (this._zoom < minZoom)
|
||||
this._zoom = this.minimumZoom;
|
||||
@@ -120,7 +118,7 @@ class Camera extends Component {
|
||||
return this;
|
||||
}
|
||||
|
||||
public setZoom(zoom: number){
|
||||
public setZoom(zoom: number): Camera{
|
||||
let newZoom = MathHelper.clamp(zoom, -1, 1);
|
||||
if (newZoom == 0){
|
||||
this._zoom = 1;
|
||||
@@ -130,63 +128,107 @@ class Camera extends Component {
|
||||
this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom);
|
||||
}
|
||||
|
||||
this._areMatrixesDirty = true;
|
||||
|
||||
SceneManager.scene.scaleX = this._zoom;
|
||||
SceneManager.scene.scaleY = this._zoom;
|
||||
return this;
|
||||
}
|
||||
|
||||
public initialize() {
|
||||
|
||||
}
|
||||
|
||||
public update(){
|
||||
|
||||
public setRotation(rotation: number): Camera {
|
||||
SceneManager.scene.rotation = rotation;
|
||||
return this;
|
||||
}
|
||||
|
||||
public setPosition(position: Vector2){
|
||||
this.entity.transform.setPosition(position);
|
||||
this.entity.position = position;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public updateMatrixes(){
|
||||
if (!this._areMatrixesDirty)
|
||||
return;
|
||||
public follow(targetEntity: Entity, cameraStyle: CameraStyle = CameraStyle.cameraWindow){
|
||||
this.targetEntity = targetEntity;
|
||||
this.cameraStyle = cameraStyle;
|
||||
let cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||
|
||||
let tempMat: Matrix2D;
|
||||
this._transformMatrix = Matrix2D.createTranslation(-this.entity.transform.position.x, -this.entity.transform.position.y);
|
||||
if (this._zoom != 1){
|
||||
tempMat = Matrix2D.createScale(this._zoom, this._zoom);
|
||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
||||
switch (this.cameraStyle){
|
||||
case CameraStyle.cameraWindow:
|
||||
let w = cameraBounds.width / 6;
|
||||
let h = cameraBounds.height / 3;
|
||||
this.deadzone = new Rectangle((cameraBounds.width - w) / 2, (cameraBounds.height - h) / 2, w, h);
|
||||
break;
|
||||
case CameraStyle.lockOn:
|
||||
this.deadzone = new Rectangle(cameraBounds.width / 2, cameraBounds.height / 2, 10, 10);
|
||||
break;
|
||||
}
|
||||
|
||||
tempMat = Matrix2D.createTranslation(this._origin.x, this._origin.y, tempMat);
|
||||
this._transformMatrix = Matrix2D.multiply(this._transformMatrix, tempMat);
|
||||
|
||||
this._inverseTransformMatrix = Matrix2D.invert(this._transformMatrix);
|
||||
|
||||
this._areBoundsDirty = true;
|
||||
this._areMatrixesDirty = false;
|
||||
}
|
||||
|
||||
public screenToWorldPoint(screenPosition: Vector2){
|
||||
this.updateMatrixes();
|
||||
return Vector2.transform(screenPosition, this._inverseTransformMatrix);
|
||||
public update(){
|
||||
let cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||
let halfScreen = Vector2.multiply(new Vector2(cameraBounds.width, cameraBounds.height), new Vector2(0.5));
|
||||
this._worldSpaceDeadZone.x = this.position.x - halfScreen.x + this.deadzone.x + this.focusOffset.x;
|
||||
this._worldSpaceDeadZone.y = this.position.y - halfScreen.y + this.deadzone.y + this.focusOffset.y;
|
||||
this._worldSpaceDeadZone.width = this.deadzone.width;
|
||||
this._worldSpaceDeadZone.height = this.deadzone.height;
|
||||
|
||||
if (this.targetEntity)
|
||||
this.updateFollow();
|
||||
|
||||
this.position = Vector2.lerp(this.position, Vector2.add(this.position, this._desiredPositionDelta), this.followLerp);
|
||||
this.entity.roundPosition();
|
||||
|
||||
if (this.mapLockEnabled){
|
||||
this.position = this.clampToMapSize(this.position);
|
||||
this.entity.roundPosition();
|
||||
}
|
||||
}
|
||||
|
||||
public worldToScreenPoint(worldPosition: Vector2){
|
||||
this.updateMatrixes();
|
||||
return Vector2.transform(worldPosition, this._transformMatrix);
|
||||
private clampToMapSize(position: Vector2){
|
||||
let cameraBounds = new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||
let halfScreen = Vector2.multiply(new Vector2(cameraBounds.width, cameraBounds.height), new Vector2(0.5));
|
||||
let cameraMax = new Vector2(this.mapSize.x - halfScreen.x, this.mapSize.y - halfScreen.y);
|
||||
|
||||
return Vector2.clamp(position, halfScreen, cameraMax);
|
||||
}
|
||||
|
||||
public destory() {
|
||||
private updateFollow(){
|
||||
this._desiredPositionDelta.x = this._desiredPositionDelta.y = 0;
|
||||
|
||||
if (this.cameraStyle == CameraStyle.lockOn){
|
||||
let targetX = this.targetEntity.position.x;
|
||||
let targetY = this.targetEntity.position.y;
|
||||
|
||||
if (this._worldSpaceDeadZone.x > targetX)
|
||||
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
||||
else if(this._worldSpaceDeadZone.x < targetX)
|
||||
this._desiredPositionDelta.x = targetX - this._worldSpaceDeadZone.x;
|
||||
|
||||
if (this._worldSpaceDeadZone.y < targetY)
|
||||
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
||||
else if(this._worldSpaceDeadZone.y > targetY)
|
||||
this._desiredPositionDelta.y = targetY - this._worldSpaceDeadZone.y;
|
||||
} else {
|
||||
if (!this._targetCollider){
|
||||
this._targetCollider = this.targetEntity.getComponent<Collider>(Collider);
|
||||
if (!this._targetCollider)
|
||||
return;
|
||||
}
|
||||
|
||||
let targetBounds = this.targetEntity.getComponent<Collider>(Collider).bounds;
|
||||
if (!this._worldSpaceDeadZone.containsRect(targetBounds)){
|
||||
if (this._worldSpaceDeadZone.left > targetBounds.left)
|
||||
this._desiredPositionDelta.x = targetBounds.left - this._worldSpaceDeadZone.left;
|
||||
else if(this._worldSpaceDeadZone.right < targetBounds.right)
|
||||
this._desiredPositionDelta.x = targetBounds.right - this._worldSpaceDeadZone.right;
|
||||
|
||||
if (this._worldSpaceDeadZone.bottom < targetBounds.bottom)
|
||||
this._desiredPositionDelta.y = targetBounds.bottom - this._worldSpaceDeadZone.bottom;
|
||||
else if(this._worldSpaceDeadZone.top > targetBounds.top)
|
||||
this._desiredPositionDelta.y = targetBounds.top - this._worldSpaceDeadZone.top;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class CameraInset {
|
||||
public left;
|
||||
public right;
|
||||
public top;
|
||||
public bottom;
|
||||
enum CameraStyle {
|
||||
lockOn,
|
||||
cameraWindow,
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
class ComponentPool<T extends PooledComponent>{
|
||||
private _cache: T[];
|
||||
private _type: any;
|
||||
|
||||
constructor(typeClass: any){
|
||||
this._type = typeClass;
|
||||
this._cache = [];
|
||||
}
|
||||
|
||||
public obtain(): T{
|
||||
try {
|
||||
return this._cache.length > 0 ? this._cache.shift() : new this._type();
|
||||
} catch(err){
|
||||
throw new Error(this._type + err);
|
||||
}
|
||||
}
|
||||
|
||||
public free(component: T){
|
||||
component.reset();
|
||||
this._cache.push(component);
|
||||
}
|
||||
}
|
||||
@@ -1,57 +1,32 @@
|
||||
class Mesh extends Component {
|
||||
private _verts: VertexPosition[];
|
||||
private _primitiveCount: number;
|
||||
private _triangles: number[];
|
||||
private _topLeftVertPosition: Vector2;
|
||||
private _width;
|
||||
private _height;
|
||||
///<reference path="./RenderableComponent.ts" />
|
||||
class Mesh extends RenderableComponent {
|
||||
private _mesh: egret.Mesh;
|
||||
|
||||
public initialize() {
|
||||
constructor(){
|
||||
super();
|
||||
|
||||
this._mesh = new egret.Mesh();
|
||||
}
|
||||
|
||||
public setVertPosition(positions: Vector2[]){
|
||||
let createVerts = !this._verts || this._verts.length != positions.length;
|
||||
if (createVerts)
|
||||
this._verts = new Array(positions.length);
|
||||
|
||||
for (let i = 0; i < this._verts.length; i ++){
|
||||
this._verts[i] = new VertexPosition(positions[i]);
|
||||
}
|
||||
public setTexture(texture: egret.Texture): Mesh{
|
||||
this._mesh.texture = texture;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public setTriangles(triangles: number[]){
|
||||
this._primitiveCount = triangles.length / 3;
|
||||
this._triangles = triangles;
|
||||
return this;
|
||||
public onAddedToEntity(){
|
||||
this.addChild(this._mesh);
|
||||
}
|
||||
|
||||
public recalculateBounds(){
|
||||
this._topLeftVertPosition = new Vector2(Number.MAX_VALUE, Number.MAX_VALUE);
|
||||
let max = new Vector2(Number.MIN_VALUE, Number.MIN_VALUE);
|
||||
|
||||
for (let i = 0; i < this._verts.length; i ++){
|
||||
this._topLeftVertPosition.x = Math.min(this._topLeftVertPosition.x, this._verts[i].position.x);
|
||||
this._topLeftVertPosition.y = Math.min(this._topLeftVertPosition.y, this._verts[i].position.y);
|
||||
max.x = Math.max(max.x, this._verts[i].position.x);
|
||||
max.y = Math.max(max.y, this._verts[i].position.y);
|
||||
}
|
||||
|
||||
this._width = max.x - this._topLeftVertPosition.x;
|
||||
this._height = max.y - this._topLeftVertPosition.y;
|
||||
|
||||
return this;
|
||||
public onRemovedFromEntity(){
|
||||
this.removeChild(this._mesh);
|
||||
}
|
||||
|
||||
public render(){
|
||||
public render(camera: Camera){
|
||||
this.x = this.entity.position.x - camera.position.x + camera.origin.x;
|
||||
this.y = this.entity.position.y - camera.position.y + camera.origin.y;
|
||||
}
|
||||
}
|
||||
|
||||
class VertexPosition{
|
||||
public position: Vector2;
|
||||
|
||||
constructor(position: Vector2){
|
||||
this.position = position;
|
||||
public reset() {
|
||||
}
|
||||
}
|
||||
@@ -8,12 +8,16 @@ class BoxCollider extends Collider {
|
||||
this.setWidth(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置BoxCollider的宽度
|
||||
* @param width
|
||||
*/
|
||||
public setWidth(width: number): BoxCollider{
|
||||
this._colliderRequiresAutoSizing = false;
|
||||
let box = this.shape as Box;
|
||||
if (width != box.width){
|
||||
// 更新框,改变边界,如果我们需要更新物理系统中的边界
|
||||
box.updateBox(width, box.height);
|
||||
this._isPositionDirty = true;
|
||||
if (this.entity && this._isParentEntityAddedToScene)
|
||||
Physics.updateCollider(this);
|
||||
}
|
||||
@@ -29,20 +33,28 @@ class BoxCollider extends Collider {
|
||||
this.setHeight(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置BoxCollider的高度
|
||||
* @param height
|
||||
*/
|
||||
public setHeight(height: number){
|
||||
this._colliderRequiresAutoSizing = false;
|
||||
let box = this.shape as Box;
|
||||
if (height != box.height){
|
||||
// 更新框,改变边界,如果我们需要更新物理系统中的边界
|
||||
box.updateBox(box.width, height);
|
||||
this._isPositionDirty = true;
|
||||
if (this.entity && this._isParentEntityAddedToScene)
|
||||
Physics.updateCollider(this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 零参数构造函数要求RenderableComponent在实体上,这样碰撞器可以在实体被添加到场景时调整自身的大小。
|
||||
*/
|
||||
constructor(){
|
||||
super();
|
||||
|
||||
// 我们在这里插入一个1x1框作为占位符,直到碰撞器在下一阵被添加到实体并可以获得更精确的自动调整大小数据
|
||||
this.shape = new Box(1, 1);
|
||||
this._colliderRequiresAutoSizing = true;
|
||||
}
|
||||
@@ -51,8 +63,8 @@ class BoxCollider extends Collider {
|
||||
this._colliderRequiresAutoSizing = false;
|
||||
let box = this.shape as Box;
|
||||
if (width != box.width || height != box.height){
|
||||
// 更新框,改变边界,如果我们需要更新物理系统中的边界
|
||||
box.updateBox(width, height);
|
||||
this._isPositionDirty = true;
|
||||
if (this.entity && this._isParentEntityAddedToScene)
|
||||
Physics.updateCollider(this);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
class CircleCollider extends Collider {
|
||||
public get radius(): number{
|
||||
return (this.shape as Circle).radius;
|
||||
}
|
||||
public set radius(value: number){
|
||||
this.setRadius(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个有半径的圆
|
||||
*
|
||||
* @param radius
|
||||
*/
|
||||
constructor(radius?: number){
|
||||
super();
|
||||
|
||||
if (radius)
|
||||
this._colliderRequiresAutoSizing = true;
|
||||
// 我们在这里插入一个1px的圆圈作为占位符
|
||||
// 直到碰撞器被添加到实体并可以获得更精确的自动调整大小数据的下一帧
|
||||
this.shape = new Circle(radius ? radius : 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置圆的半径
|
||||
* @param radius
|
||||
*/
|
||||
public setRadius(radius: number): CircleCollider{
|
||||
this._colliderRequiresAutoSizing = false;
|
||||
let circle = this.shape as Circle;
|
||||
if (radius != circle.radius){
|
||||
circle.radius = radius;
|
||||
circle._originalRadius = radius;
|
||||
|
||||
if (this.entity && this._isParentEntityAddedToScene)
|
||||
Physics.updateCollider(this);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -1,65 +1,89 @@
|
||||
abstract class Collider extends Component{
|
||||
abstract class Collider extends Component {
|
||||
/** 对撞机的基本形状 */
|
||||
public shape: Shape;
|
||||
/** 在处理冲突时,physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法。 */
|
||||
public physicsLayer = 1 << 0;
|
||||
/** 如果这个碰撞器是一个触发器,它将不会引起碰撞,但它仍然会触发事件 */
|
||||
public isTrigger: boolean;
|
||||
public registeredPhysicsBounds: Rectangle;
|
||||
public shouldColliderScaleAndRotationWithTransform = true;
|
||||
/**
|
||||
* 这个对撞机在物理系统注册时的边界。
|
||||
* 存储这个允许我们始终能够安全地从物理系统中移除对撞机,即使它在试图移除它之前已经被移动了。
|
||||
*/
|
||||
public registeredPhysicsBounds: Rectangle = new Rectangle();
|
||||
/** 如果为true,碰撞器将根据附加的变换缩放和旋转 */
|
||||
public shouldColliderScaleAndRotateWithTransform = true;
|
||||
/** 默认为所有层。 */
|
||||
public collidesWithLayers = Physics.allLayers;
|
||||
|
||||
public _localOffsetLength: number;
|
||||
public _isPositionDirty = true;
|
||||
public _isRotationDirty = true;
|
||||
/** 标记来跟踪我们的实体是否被添加到场景中 */
|
||||
protected _isParentEntityAddedToScene;
|
||||
protected _colliderRequiresAutoSizing;
|
||||
protected _localOffset: Vector2 = new Vector2(0, 0);
|
||||
/** 标记来记录我们是否注册了物理系统 */
|
||||
protected _isColliderRegistered;
|
||||
|
||||
public get bounds(): Rectangle {
|
||||
if (this._isPositionDirty || this._isRotationDirty){
|
||||
this.shape.recalculateBounds(this);
|
||||
this._isPositionDirty = this._isRotationDirty = false;
|
||||
}
|
||||
|
||||
this.shape.recalculateBounds(this);
|
||||
return this.shape.bounds;
|
||||
}
|
||||
|
||||
public get localOffset(){
|
||||
public get localOffset() {
|
||||
return this._localOffset;
|
||||
}
|
||||
|
||||
public set localOffset(value: Vector2){
|
||||
/**
|
||||
* 将localOffset添加到实体。获取碰撞器的最终位置。这允许您向一个实体添加多个碰撞器并分别定位它们。
|
||||
*/
|
||||
public set localOffset(value: Vector2) {
|
||||
this.setLocalOffset(value);
|
||||
}
|
||||
|
||||
public setLocalOffset(offset: Vector2){
|
||||
if (this._localOffset != offset){
|
||||
public setLocalOffset(offset: Vector2) {
|
||||
if (this._localOffset != offset) {
|
||||
this.unregisterColliderWithPhysicsSystem();
|
||||
this._localOffset = offset;
|
||||
this._localOffsetLength = this._localOffset.length();
|
||||
this._isPositionDirty = true;
|
||||
this.registerColliderWithPhysicsSystem();
|
||||
}
|
||||
}
|
||||
|
||||
public registerColliderWithPhysicsSystem(){
|
||||
if (this._isParentEntityAddedToScene && !this._isColliderRegistered){
|
||||
/**
|
||||
* 父实体会在不同的时间调用它(当添加到场景,启用,等等)
|
||||
*/
|
||||
public registerColliderWithPhysicsSystem() {
|
||||
// 如果在将我们添加到实体之前更改了origin等属性,则实体可以为null
|
||||
if (this._isParentEntityAddedToScene && !this._isColliderRegistered) {
|
||||
Physics.addCollider(this);
|
||||
this._isColliderRegistered = true;
|
||||
}
|
||||
}
|
||||
|
||||
public unregisterColliderWithPhysicsSystem(){
|
||||
if (this._isParentEntityAddedToScene && this._isColliderRegistered){
|
||||
/**
|
||||
* 父实体会在不同的时候调用它(从场景中移除,禁用,等等)
|
||||
*/
|
||||
public unregisterColliderWithPhysicsSystem() {
|
||||
if (this._isParentEntityAddedToScene && this._isColliderRegistered) {
|
||||
Physics.removeCollider(this);
|
||||
}
|
||||
this._isColliderRegistered = false;
|
||||
}
|
||||
|
||||
public overlaps(other: Collider){
|
||||
/**
|
||||
* 检查这个形状是否与物理系统中的其他对撞机重叠
|
||||
* @param other
|
||||
*/
|
||||
public overlaps(other: Collider) {
|
||||
return this.shape.overlaps(other.shape);
|
||||
}
|
||||
|
||||
public collidesWith(collider: Collider, motion: Vector2){
|
||||
/**
|
||||
* 检查这个与运动应用的碰撞器(移动向量)是否与碰撞器碰撞。如果是这样,将返回true,并且结果将填充碰撞数据。
|
||||
* @param collider
|
||||
* @param motion
|
||||
*/
|
||||
public collidesWith(collider: Collider, motion: Vector2) {
|
||||
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
|
||||
let oldPosition = this.shape.position;
|
||||
this.shape.position = Vector2.add(this.shape.position, motion);
|
||||
|
||||
@@ -67,66 +91,71 @@ abstract class Collider extends Component{
|
||||
if (result)
|
||||
result.collider = collider;
|
||||
|
||||
// 将图形位置返回到检查前的位置
|
||||
this.shape.position = oldPosition;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public onAddedToEntity(){
|
||||
if (this._colliderRequiresAutoSizing){
|
||||
if (!(this instanceof BoxCollider)){
|
||||
public onAddedToEntity() {
|
||||
if (this._colliderRequiresAutoSizing) {
|
||||
if (!(this instanceof BoxCollider || this instanceof CircleCollider)) {
|
||||
console.error("Only box and circle colliders can be created automatically");
|
||||
}
|
||||
|
||||
let renderable = this.entity.getComponent<RenderableComponent>(RenderableComponent);
|
||||
if (renderable){
|
||||
let renderbaleBounds = renderable.bounds;
|
||||
if (renderable) {
|
||||
let bounds = renderable.bounds;
|
||||
|
||||
let width = renderbaleBounds.width / this.entity.transform.scale.x;
|
||||
let height = renderbaleBounds.height / this.entity.transform.scale.y;
|
||||
// 这里我们需要大小*反尺度,因为当我们自动调整碰撞器的大小时,它需要没有缩放的渲染
|
||||
let width = bounds.width / this.entity.scale.x;
|
||||
let height = bounds.height / this.entity.scale.y;
|
||||
|
||||
if (this instanceof BoxCollider){
|
||||
let boxCollider = this as BoxCollider;
|
||||
// 圆碰撞器需要特别注意原点
|
||||
if (this instanceof CircleCollider){
|
||||
let circleCollider = this as CircleCollider;
|
||||
circleCollider.radius = Math.max(width, height) * 0.5;
|
||||
|
||||
this.localOffset = bounds.location;
|
||||
} else {
|
||||
let boxCollider = this;
|
||||
boxCollider.width = width;
|
||||
boxCollider.height = height;
|
||||
|
||||
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.transform.position);
|
||||
this.localOffset = bounds.location;
|
||||
}
|
||||
} else {
|
||||
console.warn("Collider has no shape and no RenderableComponent. Can't figure out how to size it.");
|
||||
}
|
||||
}
|
||||
|
||||
this._isParentEntityAddedToScene = true;
|
||||
this.registerColliderWithPhysicsSystem();
|
||||
}
|
||||
|
||||
public onRemovedFromEntity(){
|
||||
|
||||
public onRemovedFromEntity() {
|
||||
this.unregisterColliderWithPhysicsSystem();
|
||||
this._isParentEntityAddedToScene = false;
|
||||
}
|
||||
|
||||
public onEntityTransformChanged(comp: ComponentTransform){
|
||||
switch (comp){
|
||||
case ComponentTransform.position:
|
||||
this._isPositionDirty = true;
|
||||
break;
|
||||
case ComponentTransform.scale:
|
||||
this._isPositionDirty = true;
|
||||
break;
|
||||
case ComponentTransform.rotation:
|
||||
this._isRotationDirty = true;
|
||||
break;
|
||||
}
|
||||
public onEnabled() {
|
||||
this.registerColliderWithPhysicsSystem();
|
||||
}
|
||||
|
||||
public onDisabled() {
|
||||
this.unregisterColliderWithPhysicsSystem();
|
||||
}
|
||||
|
||||
public onEntityTransformChanged(comp: TransformComponent) {
|
||||
if (this._isColliderRegistered)
|
||||
Physics.updateCollider(this);
|
||||
}
|
||||
|
||||
public onEnabled(){
|
||||
this.registerColliderWithPhysicsSystem();
|
||||
this._isPositionDirty = this._isRotationDirty = true;
|
||||
}
|
||||
|
||||
public onDisabled(){
|
||||
this.unregisterColliderWithPhysicsSystem();
|
||||
public update(){
|
||||
let renderable = this.entity.getComponent<RenderableComponent>(RenderableComponent);
|
||||
if (renderable){
|
||||
this.$setX(renderable.x + this.localOffset.x);
|
||||
this.$setY(renderable.y + this.localOffset.y);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* 多边形应该以顺时针方式定义
|
||||
*/
|
||||
class PolygonCollider extends Collider {
|
||||
/**
|
||||
* 如果这些点没有居中,它们将以localOffset的差异为居中。
|
||||
* @param points
|
||||
*/
|
||||
constructor(points: Vector2[]){
|
||||
super();
|
||||
|
||||
// 第一点和最后一点决不能相同。我们想要一个开放的多边形
|
||||
let isPolygonClosed = points[0] == points[points.length - 1];
|
||||
|
||||
// 最后一个移除
|
||||
if (isPolygonClosed)
|
||||
points.splice(points.length - 1, 1);
|
||||
|
||||
let center = Polygon.findPolygonCenter(points);
|
||||
this.setLocalOffset(center);
|
||||
Polygon.recenterPolygonVerts(points);
|
||||
this.shape = new Polygon(points);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,10 @@
|
||||
/**
|
||||
* 辅助类说明了一种处理移动的方法,它考虑了包括触发器在内的所有冲突。
|
||||
* ITriggerListener接口用于管理对移动过程中违反的任何触发器的回调。
|
||||
* 一个物体只能通过移动器移动。要正确报告触发器的move方法。
|
||||
*
|
||||
* 请注意,多个移动者相互交互将多次调用ITriggerListener。
|
||||
*/
|
||||
class Mover extends Component {
|
||||
private _triggerHelper: ColliderTriggerHelper;
|
||||
|
||||
@@ -5,6 +12,10 @@ class Mover extends Component {
|
||||
this._triggerHelper = new ColliderTriggerHelper(this.entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算修改运动矢量的运动,以考虑移动时可能发生的碰撞
|
||||
* @param motion
|
||||
*/
|
||||
public calculateMovement(motion: Vector2){
|
||||
let collisionResult = new CollisionResult();
|
||||
|
||||
@@ -12,27 +23,35 @@ class Mover extends Component {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 移动所有的非触发碰撞器并获得最近的碰撞
|
||||
let colliders: Collider[] = this.entity.getComponents(Collider);
|
||||
for (let i = 0; i < colliders.length; i ++){
|
||||
let collider = colliders[i];
|
||||
|
||||
// 不检测触发器 在我们移动后会重新访问它
|
||||
if (collider.isTrigger)
|
||||
continue;
|
||||
|
||||
// 获取我们在新位置可能发生碰撞的任何东西
|
||||
let bounds = collider.bounds;
|
||||
bounds.x += motion.x;
|
||||
bounds.y += motion.y;
|
||||
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
|
||||
let boxcastResult = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
|
||||
bounds = boxcastResult.bounds;
|
||||
let neighbors = boxcastResult.tempHashSet;
|
||||
|
||||
for (let j = 0; j < neighbors.length; j ++){
|
||||
let neighbor = neighbors[j];
|
||||
// 不检测触发器
|
||||
if (neighbor.isTrigger)
|
||||
continue;
|
||||
|
||||
let _internalcollisionResult = collider.collidesWith(neighbor, motion);
|
||||
if (_internalcollisionResult){
|
||||
// 如果碰撞 则退回之前的移动量
|
||||
motion = Vector2.subtract(motion, _internalcollisionResult.minimumTranslationVector);
|
||||
|
||||
// 如果我们碰到多个对象,为了简单起见,只取第一个。
|
||||
if (_internalcollisionResult.collider){
|
||||
collisionResult = _internalcollisionResult;
|
||||
}
|
||||
@@ -42,18 +61,30 @@ class Mover extends Component {
|
||||
|
||||
ListPool.free(colliders);
|
||||
|
||||
return collisionResult;
|
||||
return {collisionResult: collisionResult, motion: motion};
|
||||
}
|
||||
|
||||
/**
|
||||
* 将calculatemomovement应用到实体并更新triggerHelper
|
||||
* @param motion
|
||||
*/
|
||||
public applyMovement(motion: Vector2){
|
||||
this.entity.transform.position = Vector2.add(this.entity.transform.position, motion);
|
||||
// 移动实体到它的新位置,如果我们有一个碰撞,否则移动全部数量。当碰撞发生时,运动被更新
|
||||
this.entity.position = Vector2.add(this.entity.position, motion);
|
||||
|
||||
// 对所有是触发器的碰撞器与所有宽相位碰撞器进行重叠检查。任何重叠都会导致触发事件。
|
||||
if (this._triggerHelper)
|
||||
this._triggerHelper.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过调用calculateMovement和applyMovement来移动考虑碰撞的实体;
|
||||
* @param motion
|
||||
*/
|
||||
public move(motion: Vector2){
|
||||
let collisionResult = this.calculateMovement(motion);
|
||||
let movementResult = this.calculateMovement(motion);
|
||||
let collisionResult = movementResult.collisionResult;
|
||||
motion = movementResult.motion;
|
||||
|
||||
this.applyMovement(motion);
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* 只向itriggerlistener报告冲突的移动器
|
||||
* 该对象将始终移动完整的距离
|
||||
*/
|
||||
class ProjectileMover extends Component {
|
||||
private _tempTriggerList: ITriggerListener[] = [];
|
||||
private _collider: Collider;
|
||||
|
||||
public onAddedToEntity(){
|
||||
this._collider = this.entity.getComponent<Collider>(Collider);
|
||||
if (!this._collider)
|
||||
console.warn("ProjectileMover has no Collider. ProjectilMover requires a Collider!");
|
||||
}
|
||||
|
||||
/**
|
||||
* 移动考虑碰撞的实体
|
||||
* @param motion
|
||||
*/
|
||||
public move(motion: Vector2): boolean{
|
||||
if (!this._collider)
|
||||
return false;
|
||||
|
||||
let didCollide = false;
|
||||
|
||||
// 获取我们在新位置可能发生碰撞的任何东西
|
||||
this.entity.position = Vector2.add(this.entity.position, motion);
|
||||
|
||||
// 获取任何可能在新位置发生碰撞的东西
|
||||
let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers);
|
||||
for (let i = 0; i < neighbors.colliders.length; i ++){
|
||||
let neighbor = neighbors.colliders[i];
|
||||
if (this._collider.overlaps(neighbor)){
|
||||
didCollide = true;
|
||||
this.notifyTriggerListeners(this._collider, neighbor);
|
||||
}
|
||||
}
|
||||
|
||||
return didCollide;
|
||||
}
|
||||
|
||||
private notifyTriggerListeners(self: Collider, other: Collider){
|
||||
// 通知我们重叠的碰撞器实体上的任何侦听器
|
||||
other.entity.getComponents("ITriggerListener", this._tempTriggerList);
|
||||
for (let i = 0; i < this._tempTriggerList.length; i ++){
|
||||
this._tempTriggerList[i].onTriggerEnter(self, other);
|
||||
}
|
||||
this._tempTriggerList.length = 0;
|
||||
|
||||
// 通知此实体上的任何侦听器
|
||||
this.entity.getComponents("ITriggerListener", this._tempTriggerList);
|
||||
for (let i = 0; i < this._tempTriggerList.length; i ++){
|
||||
this._tempTriggerList[i].onTriggerEnter(other, self);
|
||||
}
|
||||
this._tempTriggerList.length = 0;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
class PolygonMesh extends Mesh {
|
||||
constructor(points: Vector2[], arePointsCCW: boolean = true){
|
||||
super();
|
||||
|
||||
let triangulator = new Triangulator();
|
||||
triangulator.triangulate(points, arePointsCCW);
|
||||
|
||||
this.setVertPosition(points);
|
||||
this.setTriangles(triangulator.triangleIndices);
|
||||
this.recalculateBounds();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/** 回收实例的组件类型。 */
|
||||
abstract class PooledComponent extends Component {
|
||||
public abstract reset();
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
///<reference path="./PooledComponent.ts" />
|
||||
/**
|
||||
* 所有可渲染组件的基类
|
||||
*/
|
||||
abstract class RenderableComponent extends Component {
|
||||
abstract class RenderableComponent extends PooledComponent implements IRenderable {
|
||||
private _isVisible: boolean;
|
||||
private _areBoundsDirty = true;
|
||||
private _bounds: Rectangle;
|
||||
private _localOffset: Vector2;
|
||||
protected _areBoundsDirty = true;
|
||||
protected _bounds: Rectangle = new Rectangle();
|
||||
protected _localOffset: Vector2 = Vector2.zero;
|
||||
|
||||
public color: number = 0x000000;
|
||||
|
||||
public get width(){
|
||||
return this.getWidth();
|
||||
@@ -29,7 +32,7 @@ abstract class RenderableComponent extends Component {
|
||||
}
|
||||
|
||||
public get bounds(): Rectangle{
|
||||
return this.getBounds();
|
||||
return new Rectangle(this.getBounds().x, this.getBounds().y, this.getBounds().width, this.getBounds().height);
|
||||
}
|
||||
|
||||
protected getWidth(){
|
||||
@@ -40,22 +43,14 @@ abstract class RenderableComponent extends Component {
|
||||
return this.bounds.height;
|
||||
}
|
||||
|
||||
protected getBounds(){
|
||||
if (this._areBoundsDirty){
|
||||
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new Vector2(0, 0),
|
||||
this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height);
|
||||
this._areBoundsDirty = false;
|
||||
}
|
||||
|
||||
return this._bounds;
|
||||
}
|
||||
|
||||
protected onBecameVisible(){}
|
||||
|
||||
protected onBecameInvisible(){}
|
||||
|
||||
public abstract render(camera: Camera);
|
||||
|
||||
public isVisibleFromCamera(camera: Camera): boolean{
|
||||
this.isVisible = camera.bounds.intersects(this.bounds);
|
||||
this.isVisible = camera.getBounds().intersects(this.getBounds());
|
||||
return this.isVisible;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
///<reference path="./TiledSpriteRenderer.ts"/>
|
||||
class ScrollingSpriteRenderer extends TiledSpriteRenderer {
|
||||
public scrollSpeedX = 15;
|
||||
public scroolSpeedY = 0;
|
||||
private _scrollX = 0;
|
||||
private _scrollY = 0;
|
||||
|
||||
public update(){
|
||||
this._scrollX += this.scrollSpeedX * Time.deltaTime;
|
||||
this._scrollY += this.scroolSpeedY * Time.deltaTime;
|
||||
this.sourceRect.x = this._scrollX;
|
||||
this.sourceRect.y = this._scrollY;
|
||||
}
|
||||
|
||||
public render(camera: Camera) {
|
||||
if (!this.sprite)
|
||||
return;
|
||||
|
||||
super.render(camera);
|
||||
|
||||
let renderTexture = new egret.RenderTexture();
|
||||
let cacheBitmap = new egret.DisplayObjectContainer();
|
||||
cacheBitmap.removeChildren();
|
||||
cacheBitmap.addChild(this.leftTexture);
|
||||
cacheBitmap.addChild(this.rightTexture);
|
||||
|
||||
this.leftTexture.x = this.sourceRect.x;
|
||||
this.rightTexture.x = this.sourceRect.x - this.sourceRect.width;
|
||||
this.leftTexture.y = this.sourceRect.y;
|
||||
this.rightTexture.y = this.sourceRect.y;
|
||||
|
||||
cacheBitmap.cacheAsBitmap = true;
|
||||
renderTexture.drawToTexture(cacheBitmap, new egret.Rectangle(0, 0, this.sourceRect.width, this.sourceRect.height));
|
||||
|
||||
this.bitmap.texture = renderTexture;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
class Sprite {
|
||||
public texture2D: egret.Texture;
|
||||
public readonly sourceRect: Rectangle;
|
||||
public readonly center: Vector2;
|
||||
public origin: Vector2;
|
||||
public readonly uvs: Rectangle = new Rectangle();
|
||||
|
||||
constructor(texture: egret.Texture,
|
||||
sourceRect: Rectangle = new Rectangle(0, 0, texture.textureWidth, texture.textureHeight),
|
||||
origin: Vector2 = sourceRect.getHalfSize()) {
|
||||
this.texture2D = texture;
|
||||
this.sourceRect = sourceRect;
|
||||
this.center = new Vector2(sourceRect.width * 0.5, sourceRect.height * 0.5);
|
||||
this.origin = origin;
|
||||
|
||||
let inverseTexW = 1 / texture.textureWidth;
|
||||
let inverseTexH = 1 / texture.textureHeight
|
||||
|
||||
this.uvs.x = sourceRect.x * inverseTexW;
|
||||
this.uvs.y = sourceRect.y * inverseTexH;
|
||||
this.uvs.width = sourceRect.width * inverseTexW;
|
||||
this.uvs.height = sourceRect.height * inverseTexH;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
class SpriteAnimation {
|
||||
public readonly sprites: Sprite[];
|
||||
public readonly frameRate: number;
|
||||
|
||||
constructor(sprites: Sprite[], frameRate: number){
|
||||
this.sprites = sprites;
|
||||
this.frameRate = frameRate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
///<reference path="./SpriteRenderer.ts" />
|
||||
class SpriteAnimator extends SpriteRenderer {
|
||||
/** 在动画完成时触发,包括动画名称; */
|
||||
public onAnimationCompletedEvent: Function;
|
||||
/** 动画播放速度 */
|
||||
public speed = 1;
|
||||
/** 动画的当前状态 */
|
||||
public animationState = State.none;
|
||||
/** 当前动画 */
|
||||
public currentAnimation: SpriteAnimation;
|
||||
/** 当前动画的名称 */
|
||||
public currentAnimationName: string;
|
||||
/** 当前动画的精灵数组中当前帧的索引 */
|
||||
public currentFrame: number;
|
||||
/** 检查当前动画是否正在运行 */
|
||||
public get isRunning(): boolean{
|
||||
return this.animationState == State.running;
|
||||
}
|
||||
|
||||
private _animations: Map<string, SpriteAnimation> = new Map<string, SpriteAnimation>();
|
||||
private _elapsedTime: number = 0;
|
||||
private _loopMode: LoopMode;
|
||||
|
||||
constructor(sprite?: Sprite){
|
||||
super();
|
||||
|
||||
if (sprite) this.setSprite(sprite);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个SpriteAnimation
|
||||
* @param name
|
||||
* @param animation
|
||||
*/
|
||||
public addAnimation(name: string, animation: SpriteAnimation): SpriteAnimator{
|
||||
if (!this.sprite && animation.sprites.length > 0)
|
||||
this.setSprite(animation.sprites[0]);
|
||||
this._animations[name] = animation;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 以给定的名称放置动画。如果没有指定循环模式,则默认为循环
|
||||
* @param name
|
||||
* @param loopMode
|
||||
*/
|
||||
public play(name: string, loopMode: LoopMode = null){
|
||||
this.currentAnimation = this._animations[name];
|
||||
this.currentAnimationName = name;
|
||||
this.currentFrame = 0;
|
||||
this.animationState = State.running;
|
||||
|
||||
this.sprite = this.currentAnimation.sprites[0];
|
||||
this._elapsedTime = 0;
|
||||
this._loopMode = loopMode ? loopMode : LoopMode.loop;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查动画是否正在播放(即动画是活动的)。它可能仍然处于暂停状态)
|
||||
* @param name
|
||||
*/
|
||||
public isAnimationActive(name: string): boolean{
|
||||
return this.currentAnimation && this.currentAnimationName == name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 暂停动画
|
||||
*/
|
||||
public pause(){
|
||||
this.animationState = State.paused;
|
||||
}
|
||||
|
||||
/**
|
||||
* 继续动画
|
||||
*/
|
||||
public unPause(){
|
||||
this.animationState = State.running;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止当前动画并将其设为null
|
||||
*/
|
||||
public stop(){
|
||||
this.currentAnimation = null;
|
||||
this.currentAnimationName = null;
|
||||
this.currentFrame = 0;
|
||||
this.animationState = State.none;
|
||||
}
|
||||
|
||||
public update(){
|
||||
if (this.animationState != State.running || !this.currentAnimation) return;
|
||||
|
||||
let animation = this.currentAnimation;
|
||||
let secondsPerFrame = 1 / (animation.frameRate * this.speed);
|
||||
let iterationDuration = secondsPerFrame * animation.sprites.length;
|
||||
|
||||
this._elapsedTime += Time.deltaTime;
|
||||
let time = Math.abs(this._elapsedTime);
|
||||
|
||||
if (this._loopMode == LoopMode.once && time > iterationDuration ||
|
||||
this._loopMode == LoopMode.pingPongOnce && time > iterationDuration * 2){
|
||||
this.animationState = State.completed;
|
||||
this._elapsedTime = 0;
|
||||
this.currentFrame = 0;
|
||||
this.sprite = animation.sprites[this.currentFrame];
|
||||
return;
|
||||
}
|
||||
|
||||
// 弄清楚我们在哪个坐标系上
|
||||
let i = Math.floor(time / secondsPerFrame);
|
||||
let n = animation.sprites.length;
|
||||
if (n > 2 && (this._loopMode == LoopMode.pingPong || this._loopMode == LoopMode.pingPongOnce)){
|
||||
// pingpong
|
||||
let maxIndex = n - 1;
|
||||
this.currentFrame = maxIndex - Math.abs(maxIndex - i % (maxIndex * 2));
|
||||
}else{
|
||||
this.currentFrame = i % n;
|
||||
}
|
||||
|
||||
this.sprite = animation.sprites[this.currentFrame];
|
||||
}
|
||||
}
|
||||
|
||||
enum LoopMode {
|
||||
/** 在一个循环序列[A][B][C][A][B][C][A][B][C]... */
|
||||
loop,
|
||||
/** [A][B][C]然后暂停,设置时间为0 [A] */
|
||||
once,
|
||||
/** [A][B][C]。当它到达终点时,它会继续播放最后一帧,并且不会停止播放 */
|
||||
clampForever,
|
||||
/** 以一个乒乓循环的方式永远播放这个序列 [A][B][C][B][A][B][C][B]... */
|
||||
pingPong,
|
||||
/** 将顺序向前播放一次,然后返回到开始[A][B][C][B][A],然后暂停并设置时间为0 */
|
||||
pingPongOnce,
|
||||
}
|
||||
|
||||
enum State {
|
||||
none,
|
||||
running,
|
||||
paused,
|
||||
completed,
|
||||
}
|
||||
@@ -1,24 +1,62 @@
|
||||
class SpriteRenderer extends RenderableComponent {
|
||||
private _sprite: egret.DisplayObject;
|
||||
private _origin: Vector2;
|
||||
class SpriteRenderer extends RenderableComponent{
|
||||
private _sprite: Sprite;
|
||||
protected bitmap: egret.Bitmap;
|
||||
|
||||
public get sprite(){
|
||||
/** 应该由这个精灵显示的精灵 */
|
||||
public get sprite(): Sprite{
|
||||
return this._sprite;
|
||||
}
|
||||
|
||||
public set sprite(value: egret.DisplayObject){
|
||||
/** 应该由这个精灵显示的精灵 */
|
||||
public set sprite(value: Sprite){
|
||||
this.setSprite(value);
|
||||
}
|
||||
|
||||
public setSprite(sprite: egret.DisplayObject): SpriteRenderer{
|
||||
public setSprite(sprite: Sprite): SpriteRenderer{
|
||||
this.removeChildren();
|
||||
this._sprite = sprite;
|
||||
if (this._sprite)
|
||||
this._origin = new Vector2(this._sprite.anchorOffsetX, this._sprite.anchorOffsetY);
|
||||
if (this._sprite) {
|
||||
this.anchorOffsetX = this._sprite.origin.x / this._sprite.sourceRect.width;
|
||||
this.anchorOffsetY = this._sprite.origin.y / this._sprite.sourceRect.height;
|
||||
}
|
||||
this.bitmap = new egret.Bitmap(sprite.texture2D);
|
||||
this.addChild(this.bitmap);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public initialize() {
|
||||
public setColor(color: number): SpriteRenderer{
|
||||
let colorMatrix = [
|
||||
1, 0, 0, 0, 0,
|
||||
0, 1, 0, 0, 0,
|
||||
0, 0, 1, 0, 0,
|
||||
0, 0, 0, 1, 0
|
||||
];
|
||||
colorMatrix[0] = Math.floor(color / 256 / 256) / 255;
|
||||
colorMatrix[6] = Math.floor(color / 256 % 256) / 255;
|
||||
colorMatrix[12] = color % 256 / 255;
|
||||
let colorFilter = new egret.ColorMatrixFilter(colorMatrix);
|
||||
this.filters = [colorFilter];
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public isVisibleFromCamera(camera: Camera): boolean{
|
||||
this.isVisible = new Rectangle(0, 0, this.stage.stageWidth, this.stage.stageHeight).intersects(this.bounds);
|
||||
this.visible = this.isVisible;
|
||||
return this.isVisible;
|
||||
}
|
||||
|
||||
/** 渲染处理 在每个模块中处理各自的渲染逻辑 */
|
||||
public render(camera: Camera){
|
||||
this.x = -camera.position.x + camera.origin.x;
|
||||
this.y = -camera.position.y + camera.origin.y;
|
||||
}
|
||||
|
||||
public onRemovedFromEntity(){
|
||||
if (this.parent)
|
||||
this.parent.removeChild(this);
|
||||
}
|
||||
|
||||
public reset(){
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
///<reference path="./SpriteRenderer.ts" />
|
||||
/**
|
||||
* 滚动由两张图片组合而成
|
||||
*/
|
||||
class TiledSpriteRenderer extends SpriteRenderer {
|
||||
protected sourceRect: Rectangle;
|
||||
protected leftTexture: egret.Bitmap;
|
||||
protected rightTexture: egret.Bitmap;
|
||||
|
||||
public get scrollX() {
|
||||
return this.sourceRect.x;
|
||||
}
|
||||
public set scrollX(value: number) {
|
||||
this.sourceRect.x = value;
|
||||
}
|
||||
public get scrollY() {
|
||||
return this.sourceRect.y;
|
||||
}
|
||||
public set scrollY(value: number) {
|
||||
this.sourceRect.y = value;
|
||||
}
|
||||
|
||||
constructor(sprite: Sprite) {
|
||||
super();
|
||||
|
||||
this.leftTexture = new egret.Bitmap();
|
||||
this.rightTexture = new egret.Bitmap();
|
||||
this.leftTexture.texture = sprite.texture2D;
|
||||
this.rightTexture.texture = sprite.texture2D;
|
||||
|
||||
this.setSprite(sprite);
|
||||
this.sourceRect = sprite.sourceRect;
|
||||
}
|
||||
|
||||
public render(camera: Camera) {
|
||||
if (!this.sprite)
|
||||
return;
|
||||
|
||||
super.render(camera);
|
||||
|
||||
let renderTexture = new egret.RenderTexture();
|
||||
let cacheBitmap = new egret.DisplayObjectContainer();
|
||||
cacheBitmap.removeChildren();
|
||||
cacheBitmap.addChild(this.leftTexture);
|
||||
cacheBitmap.addChild(this.rightTexture);
|
||||
|
||||
this.leftTexture.x = this.sourceRect.x;
|
||||
this.rightTexture.x = this.sourceRect.x - this.sourceRect.width;
|
||||
this.leftTexture.y = this.sourceRect.y;
|
||||
this.rightTexture.y = this.sourceRect.y;
|
||||
|
||||
cacheBitmap.cacheAsBitmap = true;
|
||||
renderTexture.drawToTexture(cacheBitmap, new egret.Rectangle(0, 0, this.sourceRect.width, this.sourceRect.height));
|
||||
|
||||
this.bitmap.texture = renderTexture;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
enum CoreEvents{
|
||||
/** 当场景发生变化时触发 */
|
||||
SceneChanged,
|
||||
}
|
||||
@@ -1,107 +1,50 @@
|
||||
class Entity {
|
||||
class Entity extends egret.DisplayObjectContainer {
|
||||
private static _idGenerator: number;
|
||||
|
||||
public name: string;
|
||||
public readonly id: number;
|
||||
/** 当前实体所属的场景 */
|
||||
public scene: Scene;
|
||||
/** 封装实体的位置/旋转/缩放,并允许设置一个高层结构 */
|
||||
public readonly transform: Transform;
|
||||
/** 当前附加到此实体的所有组件的列表 */
|
||||
public readonly components: ComponentList;
|
||||
private _updateOrder: number = 0;
|
||||
private _enabled: boolean = true;
|
||||
private _isDestoryed: boolean;
|
||||
public _isDestoryed: boolean;
|
||||
private _tag: number = 0;
|
||||
|
||||
public componentBits: BitSet;
|
||||
|
||||
public get parent(){
|
||||
return this.transform.parent;
|
||||
}
|
||||
|
||||
public set parent(value: Transform){
|
||||
this.transform.setParent(value);
|
||||
public get isDestoryed(){
|
||||
return this._isDestoryed;
|
||||
}
|
||||
|
||||
public get position(){
|
||||
return this.transform.position;
|
||||
return new Vector2(this.x, this.y);
|
||||
}
|
||||
|
||||
public set position(value: Vector2){
|
||||
this.transform.setPosition(value);
|
||||
}
|
||||
|
||||
public get localPosition(){
|
||||
return this.transform.localPosition;
|
||||
}
|
||||
|
||||
public set localPosition(value: Vector2){
|
||||
this.transform.setLocalPosition(value);
|
||||
}
|
||||
|
||||
public get rotation(){
|
||||
return this.transform.rotation;
|
||||
}
|
||||
|
||||
public set rotation(value: number){
|
||||
this.transform.setRotation(value);
|
||||
}
|
||||
|
||||
public get rotationDegrees(){
|
||||
return this.transform.rotationDegrees;
|
||||
}
|
||||
|
||||
public set rotationDegrees(value: number){
|
||||
this.transform.setRotationDegrees(value);
|
||||
}
|
||||
|
||||
public get localRotation(){
|
||||
return this.transform.localRotation;
|
||||
}
|
||||
|
||||
public set localRotation(value: number){
|
||||
this.transform.setLocalRotation(value);
|
||||
}
|
||||
|
||||
public get localRotationDegrees(){
|
||||
return this.transform.localRotationDegrees;
|
||||
}
|
||||
|
||||
public set localRotationDegrees(value: number){
|
||||
this.transform.setLocalRotationDegrees(value);
|
||||
this.$setX(value.x);
|
||||
this.$setY(value.y);
|
||||
this.onEntityTransformChanged(TransformComponent.position);
|
||||
}
|
||||
|
||||
public get scale(){
|
||||
return this.transform.scale;
|
||||
return new Vector2(this.scaleX, this.scaleY);
|
||||
}
|
||||
|
||||
public set scale(value: Vector2){
|
||||
this.transform.setScale(value);
|
||||
this.$setScaleX(value.x);
|
||||
this.$setScaleY(value.y);
|
||||
this.onEntityTransformChanged(TransformComponent.scale);
|
||||
}
|
||||
|
||||
public get localScale(){
|
||||
return this.transform.scale;
|
||||
public set rotation(value: number){
|
||||
this.$setRotation(value);
|
||||
this.onEntityTransformChanged(TransformComponent.rotation);
|
||||
}
|
||||
|
||||
public set localScale(value: Vector2){
|
||||
this.transform.setScale(value);
|
||||
}
|
||||
|
||||
public get worldInverseTransform(){
|
||||
return this.transform.worldInverseTransform;
|
||||
}
|
||||
|
||||
public get localToWorldTransform(){
|
||||
return this.transform.localToWorldTransform;
|
||||
}
|
||||
|
||||
public get worldToLocalTransform(){
|
||||
return this.transform.worldToLocalTransform;
|
||||
}
|
||||
|
||||
public get isDestoryed(){
|
||||
return this._isDestoryed;
|
||||
public get rotation(){
|
||||
return this.$getRotation();
|
||||
}
|
||||
|
||||
public get enabled(){
|
||||
@@ -128,13 +71,25 @@ class Entity {
|
||||
this.setTag(value);
|
||||
}
|
||||
|
||||
public get stage(){
|
||||
if (!this.scene)
|
||||
return null;
|
||||
|
||||
return this.scene.stage;
|
||||
}
|
||||
|
||||
constructor(name: string){
|
||||
super();
|
||||
this.name = name;
|
||||
this.transform = new Transform(this);
|
||||
this.components = new ComponentList(this);
|
||||
this.id = Entity._idGenerator ++;
|
||||
|
||||
this.componentBits = new BitSet();
|
||||
this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
|
||||
}
|
||||
|
||||
private onAddToStage(){
|
||||
this.onEntityTransformChanged(TransformComponent.position);
|
||||
}
|
||||
|
||||
public get updateOrder(){
|
||||
@@ -145,6 +100,10 @@ class Entity {
|
||||
this.setUpdateOrder(value);
|
||||
}
|
||||
|
||||
public roundPosition(){
|
||||
this.position = Vector2Ext.round(this.position);
|
||||
}
|
||||
|
||||
public setUpdateOrder(updateOrder: number){
|
||||
if (this._updateOrder != updateOrder){
|
||||
this._updateOrder = updateOrder;
|
||||
@@ -175,8 +134,8 @@ class Entity {
|
||||
newScene.entities.add(this);
|
||||
this.components.registerAllComponents();
|
||||
|
||||
for (let i = 0; i < this.transform.childCount; i ++){
|
||||
this.transform.getChild(i).entity.attachToScene(newScene);
|
||||
for (let i = 0; i < this.numChildren; i ++){
|
||||
(this.getChildAt(i) as Component).entity.attachToScene(newScene);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,13 +143,14 @@ class Entity {
|
||||
this.scene.entities.remove(this);
|
||||
this.components.deregisterAllComponents();
|
||||
|
||||
for (let i = 0; i < this.transform.childCount; i ++)
|
||||
this.transform.getChild(i).entity.detachFromScene();
|
||||
for (let i = 0; i < this.numChildren; i ++)
|
||||
(this.getChildAt(i) as Component).entity.detachFromScene();
|
||||
}
|
||||
|
||||
public addComponent<T extends Component>(component: T): T{
|
||||
component.entity = this;
|
||||
this.components.add(component);
|
||||
this.addChild(component);
|
||||
component.initialize();
|
||||
return component;
|
||||
}
|
||||
@@ -216,6 +176,10 @@ class Entity {
|
||||
return this.components.getComponents(typeName, componentList);
|
||||
}
|
||||
|
||||
private onEntityTransformChanged(comp: TransformComponent){
|
||||
this.components.onEntityTransformChanged(comp);
|
||||
}
|
||||
|
||||
public removeComponentForType<T extends Component>(type){
|
||||
let comp = this.getComponent<T>(type);
|
||||
if (comp){
|
||||
@@ -238,7 +202,6 @@ class Entity {
|
||||
|
||||
public update(){
|
||||
this.components.update();
|
||||
this.transform.updateTransform();
|
||||
}
|
||||
|
||||
public onAddedToScene(){
|
||||
@@ -247,21 +210,28 @@ class Entity {
|
||||
|
||||
public onRemovedFromScene(){
|
||||
if (this._isDestoryed)
|
||||
this.components.remove
|
||||
this.components.removeAllComponents();
|
||||
}
|
||||
|
||||
public onTransformChanged(comp: ComponentTransform){
|
||||
this.components.onEntityTransformChanged(comp);
|
||||
}
|
||||
|
||||
public destory(){
|
||||
public destroy(){
|
||||
this._isDestoryed = true;
|
||||
this.scene.entities.remove(this);
|
||||
this.transform.parent = null;
|
||||
this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
|
||||
|
||||
for (let i = this.transform.childCount - 1; i >= 0; i --){
|
||||
let child = this.transform.getChild(i);
|
||||
child.entity.destory();
|
||||
this.scene.entities.remove(this);
|
||||
this.removeChildren();
|
||||
|
||||
if (this.parent)
|
||||
this.parent.removeChild(this);
|
||||
|
||||
for (let i = this.numChildren - 1; i >= 0; i --){
|
||||
let child = this.getChildAt(i);
|
||||
(child as Component).entity.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum TransformComponent {
|
||||
rotation,
|
||||
scale,
|
||||
position
|
||||
}
|
||||
@@ -1,49 +1,54 @@
|
||||
/** 场景 */
|
||||
class Scene extends egret.DisplayObjectContainer {
|
||||
public camera: Camera;
|
||||
public camera: Camera;
|
||||
public readonly entities: EntityList;
|
||||
public readonly renderableComponents: RenderableComponentList;
|
||||
public readonly content: ContentManager;
|
||||
public enablePostProcessing = true;
|
||||
|
||||
private _projectionMatrix: Matrix2D;
|
||||
private _transformMatrix: Matrix2D;
|
||||
private _matrixTransformMatrix: Matrix2D;
|
||||
private _renderers: Renderer[] = [];
|
||||
private _postProcessors: PostProcessor[] = [];
|
||||
private _didSceneBegin;
|
||||
|
||||
public readonly entityProcessors: EntityProcessorList;
|
||||
|
||||
constructor(displayObject: egret.DisplayObject){
|
||||
constructor() {
|
||||
super();
|
||||
displayObject.stage.addChild(this);
|
||||
this._projectionMatrix = new Matrix2D(0, 0, 0, 0, 0, 0);
|
||||
this.entityProcessors = new EntityProcessorList();
|
||||
this.renderableComponents = new RenderableComponentList();
|
||||
this.entities = new EntityList(this);
|
||||
|
||||
this.content = new ContentManager();
|
||||
this.width = SceneManager.stage.stageWidth;
|
||||
this.height = SceneManager.stage.stageHeight;
|
||||
|
||||
this.addEventListener(egret.Event.ACTIVATE, this.onActive, this);
|
||||
this.addEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
|
||||
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
|
||||
}
|
||||
|
||||
public createEntity(name: string){
|
||||
public createEntity(name: string) {
|
||||
let entity = new Entity(name);
|
||||
entity.transform.position = new Vector2(0, 0);
|
||||
entity.position = new Vector2(0, 0);
|
||||
return this.addEntity(entity);
|
||||
}
|
||||
|
||||
public addEntity(entity: Entity){
|
||||
public addEntity(entity: Entity) {
|
||||
this.entities.add(entity);
|
||||
entity.scene = this;
|
||||
this.addChild(entity);
|
||||
|
||||
for (let i = 0; i < entity.transform.childCount; i ++)
|
||||
this.addEntity(entity.transform.getChild(i).entity);
|
||||
for (let i = 0; i < entity.numChildren; i++)
|
||||
this.addEntity((entity.getChildAt(i) as Component).entity);
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
public destroyAllEntities(){
|
||||
for (let i = 0; i < this.entities.count; i ++){
|
||||
this.entities.buffer[i].destory();
|
||||
public destroyAllEntities() {
|
||||
for (let i = 0; i < this.entities.count; i++) {
|
||||
this.entities.buffer[i].destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public findEntity(name: string): Entity{
|
||||
public findEntity(name: string): Entity {
|
||||
return this.entities.findEntity(name);
|
||||
}
|
||||
|
||||
@@ -51,13 +56,13 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
* 在场景中添加一个EntitySystem处理器
|
||||
* @param processor 处理器
|
||||
*/
|
||||
public addEntityProcessor(processor: EntitySystem){
|
||||
public addEntityProcessor(processor: EntitySystem) {
|
||||
processor.scene = this;
|
||||
this.entityProcessors.add(processor);
|
||||
return processor;
|
||||
}
|
||||
|
||||
public removeEntityProcessor(processor: EntitySystem){
|
||||
public removeEntityProcessor(processor: EntitySystem) {
|
||||
this.entityProcessors.remove(processor);
|
||||
}
|
||||
|
||||
@@ -65,14 +70,40 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
return this.entityProcessors.getProcessor<T>();
|
||||
}
|
||||
|
||||
public setActive(): Scene{
|
||||
SceneManager.setActiveScene(this);
|
||||
public addRenderer<T extends Renderer>(renderer: T) {
|
||||
this._renderers.push(renderer);
|
||||
this._renderers.sort();
|
||||
|
||||
return this;
|
||||
renderer.onAddedToScene(this);
|
||||
|
||||
return renderer;
|
||||
}
|
||||
|
||||
/** 初始化场景 */
|
||||
public initialize(){
|
||||
public getRenderer<T extends Renderer>(type): T {
|
||||
for (let i = 0; i < this._renderers.length; i++) {
|
||||
if (this._renderers[i] instanceof type)
|
||||
return this._renderers[i] as T;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public removeRenderer(renderer: Renderer) {
|
||||
this._renderers.remove(renderer);
|
||||
renderer.unload();
|
||||
}
|
||||
|
||||
public begin() {
|
||||
if (SceneManager.sceneTransition){
|
||||
SceneManager.stage.addChildAt(this, SceneManager.stage.numChildren - 1);
|
||||
}else{
|
||||
SceneManager.stage.addChild(this);
|
||||
}
|
||||
|
||||
if (this._renderers.length == 0) {
|
||||
this.addRenderer(new DefaultRenderer());
|
||||
console.warn("场景开始时没有渲染器 自动添加DefaultRenderer以保证能够正常渲染");
|
||||
}
|
||||
/** 初始化默认相机 */
|
||||
this.camera = this.createEntity("camera").getOrCreateComponent(new Camera());
|
||||
|
||||
@@ -80,21 +111,61 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
|
||||
if (this.entityProcessors)
|
||||
this.entityProcessors.begin();
|
||||
|
||||
this.camera.onSceneSizeChanged(this.stage.stageWidth, this.stage.stageHeight);
|
||||
|
||||
this._didSceneBegin = true;
|
||||
this.onStart();
|
||||
}
|
||||
|
||||
public end() {
|
||||
this._didSceneBegin = false;
|
||||
|
||||
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
|
||||
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
|
||||
|
||||
for (let i = 0; i < this._renderers.length; i++) {
|
||||
this._renderers[i].unload();
|
||||
}
|
||||
|
||||
for (let i = 0; i < this._postProcessors.length; i++) {
|
||||
this._postProcessors[i].unload();
|
||||
}
|
||||
|
||||
this.entities.removeAllEntities();
|
||||
this.removeChildren();
|
||||
|
||||
Physics.clear();
|
||||
|
||||
this.camera = null;
|
||||
this.content.dispose();
|
||||
|
||||
if (this.entityProcessors)
|
||||
this.entityProcessors.end();
|
||||
|
||||
this.unload();
|
||||
|
||||
if (this.parent)
|
||||
this.parent.removeChild(this);
|
||||
}
|
||||
|
||||
protected async onStart() {
|
||||
|
||||
}
|
||||
|
||||
/** 场景激活 */
|
||||
public onActive(){
|
||||
|
||||
protected onActive() {
|
||||
|
||||
}
|
||||
|
||||
/** 场景失去焦点 */
|
||||
public onDeactive(){
|
||||
protected onDeactive() {
|
||||
|
||||
}
|
||||
|
||||
public update(){
|
||||
Time.update(egret.getTimer());
|
||||
protected unload() { }
|
||||
|
||||
public update() {
|
||||
this.entities.updateLists();
|
||||
|
||||
if (this.entityProcessors)
|
||||
@@ -104,23 +175,39 @@ class Scene extends egret.DisplayObjectContainer {
|
||||
|
||||
if (this.entityProcessors)
|
||||
this.entityProcessors.lateUpdate();
|
||||
|
||||
this.renderableComponents.updateList();
|
||||
}
|
||||
|
||||
public prepRenderState(){
|
||||
this._projectionMatrix.m11 = 2 / this.stage.width;
|
||||
this._projectionMatrix.m22 = -2 / this.stage.height;
|
||||
public postRender() {
|
||||
let enabledCounter = 0;
|
||||
if (this.enablePostProcessing) {
|
||||
for (let i = 0; i < this._postProcessors.length; i++) {
|
||||
if (this._postProcessors[i].enable) {
|
||||
let isEven = MathHelper.isEven(enabledCounter);
|
||||
enabledCounter ++;
|
||||
|
||||
this._transformMatrix = this.camera.transformMatrix;
|
||||
this._matrixTransformMatrix = Matrix2D.multiply(this._transformMatrix, this._projectionMatrix);
|
||||
this._postProcessors[i].process();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public destory(){
|
||||
this.removeEventListener(egret.Event.DEACTIVATE, this.onDeactive, this);
|
||||
this.removeEventListener(egret.Event.ACTIVATE, this.onActive, this);
|
||||
public render() {
|
||||
for (let i = 0; i < this._renderers.length; i++) {
|
||||
this._renderers[i].render(this);
|
||||
}
|
||||
}
|
||||
|
||||
this.camera.destory();
|
||||
this.camera = null;
|
||||
public addPostProcessor<T extends PostProcessor>(postProcessor: T): T{
|
||||
this._postProcessors.push(postProcessor);
|
||||
this._postProcessors.sort();
|
||||
postProcessor.onAddedToScene(this);
|
||||
|
||||
this.entities.removeAllEntities();
|
||||
if (this._didSceneBegin){
|
||||
postProcessor.onSceneBackBufferSizeChanged(this.stage.stageWidth, this.stage.stageHeight);
|
||||
}
|
||||
|
||||
return postProcessor;
|
||||
}
|
||||
}
|
||||
@@ -1,40 +1,131 @@
|
||||
/** 运行时的场景管理。 */
|
||||
class SceneManager {
|
||||
private static _loadedScenes: Map<string, Scene> = new Map();
|
||||
/** 上一个场景 */
|
||||
private static _lastScene: Scene;
|
||||
/** 当前激活的场景 */
|
||||
private static _activeScene: Scene;
|
||||
|
||||
/**
|
||||
* 使用给定的名称在运行时创建一个空的新场景。
|
||||
* 新场景将与当前打开的任何现有场景一起被添加到层次结构中。
|
||||
* 这个函数用于在运行时创建场景。
|
||||
* @param name
|
||||
* @param scene
|
||||
*/
|
||||
public static createScene(name: string, scene: Scene){
|
||||
scene.name = name;
|
||||
this._loadedScenes.set(name, scene);
|
||||
return scene;
|
||||
private static _scene: Scene;
|
||||
private static _nextScene: Scene;
|
||||
public static sceneTransition: SceneTransition;
|
||||
public static stage: egret.Stage;
|
||||
/** 订阅此事件以在活动场景发生更改时得到通知。 */
|
||||
public static activeSceneChanged: Function;
|
||||
/** 核心发射器。只发出核心级别的事件 */
|
||||
public static emitter: Emitter<CoreEvents>;
|
||||
/** 全局内容管理器加载任何应该停留在场景之间的资产 */
|
||||
public static content: ContentManager;
|
||||
/** 简化对内部类的全局内容实例的访问 */
|
||||
private static _instnace: SceneManager;
|
||||
public static get Instance(){
|
||||
return this._instnace;
|
||||
}
|
||||
|
||||
public static setActiveScene(scene: Scene){
|
||||
if (this._activeScene){
|
||||
// 如果场景相同则不进行切换
|
||||
if (this._activeScene == scene)
|
||||
return;
|
||||
constructor(stage: egret.Stage) {
|
||||
stage.addEventListener(egret.Event.ENTER_FRAME, SceneManager.update, this);
|
||||
|
||||
this._lastScene = this._activeScene;
|
||||
this._activeScene.destory();
|
||||
SceneManager._instnace = this;
|
||||
SceneManager.emitter = new Emitter<CoreEvents>();
|
||||
SceneManager.content = new ContentManager();
|
||||
|
||||
SceneManager.stage = stage;
|
||||
SceneManager.initialize(stage);
|
||||
}
|
||||
|
||||
public static get scene() {
|
||||
return this._scene;
|
||||
}
|
||||
public static set scene(value: Scene) {
|
||||
if (!value)
|
||||
throw new Error("场景不能为空");
|
||||
|
||||
if (this._scene == null) {
|
||||
this._scene = value;
|
||||
this._scene.begin();
|
||||
SceneManager.Instance.onSceneChanged();
|
||||
} else {
|
||||
this._nextScene = value;
|
||||
}
|
||||
|
||||
this._activeScene = scene;
|
||||
this._activeScene.initialize();
|
||||
return scene;
|
||||
this.registerActiveSceneChanged(this._scene, this._nextScene);
|
||||
}
|
||||
|
||||
public static getActiveScene(){
|
||||
return this._activeScene;
|
||||
public static initialize(stage: egret.Stage) {
|
||||
Input.initialize(stage);
|
||||
}
|
||||
|
||||
public static update() {
|
||||
Time.update(egret.getTimer());
|
||||
|
||||
if (SceneManager._scene) {
|
||||
for (let i = GlobalManager.globalManagers.length - 1; i >= 0; i--) {
|
||||
if (GlobalManager.globalManagers[i].enabled)
|
||||
GlobalManager.globalManagers[i].update();
|
||||
}
|
||||
|
||||
if (!SceneManager.sceneTransition ||
|
||||
(SceneManager.sceneTransition && (!SceneManager.sceneTransition.loadsNewScene || SceneManager.sceneTransition.isNewSceneLoaded))) {
|
||||
SceneManager._scene.update();
|
||||
}
|
||||
|
||||
if (SceneManager._nextScene) {
|
||||
SceneManager._scene.end();
|
||||
|
||||
SceneManager._scene = SceneManager._nextScene;
|
||||
SceneManager._nextScene = null;
|
||||
SceneManager._instnace.onSceneChanged();
|
||||
|
||||
SceneManager._scene.begin();
|
||||
}
|
||||
}
|
||||
|
||||
SceneManager.render();
|
||||
}
|
||||
|
||||
public static render() {
|
||||
if (this.sceneTransition){
|
||||
this.sceneTransition.preRender();
|
||||
|
||||
if (this._scene && !this.sceneTransition.hasPreviousSceneRender){
|
||||
this._scene.render();
|
||||
this._scene.postRender();
|
||||
this.sceneTransition.onBeginTransition();
|
||||
} else if (this.sceneTransition) {
|
||||
if (this._scene && this.sceneTransition.isNewSceneLoaded) {
|
||||
this._scene.render();
|
||||
this._scene.postRender();
|
||||
}
|
||||
|
||||
this.sceneTransition.render();
|
||||
}
|
||||
} else if (this._scene) {
|
||||
this._scene.render();
|
||||
|
||||
Debug.render();
|
||||
|
||||
this._scene.postRender();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 临时运行SceneTransition,允许一个场景过渡到另一个平滑的自定义效果。
|
||||
* @param sceneTransition
|
||||
*/
|
||||
public static startSceneTransition<T extends SceneTransition>(sceneTransition: T): T {
|
||||
if (this.sceneTransition) {
|
||||
console.warn("在前一个场景完成之前,不能开始一个新的场景转换。");
|
||||
return;
|
||||
}
|
||||
|
||||
this.sceneTransition = sceneTransition;
|
||||
return sceneTransition;
|
||||
}
|
||||
|
||||
public static registerActiveSceneChanged(current: Scene, next: Scene){
|
||||
if (this.activeSceneChanged)
|
||||
this.activeSceneChanged(current, next);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在一个场景结束后,下一个场景开始之前调用
|
||||
*/
|
||||
public onSceneChanged(){
|
||||
SceneManager.emitter.emit(CoreEvents.SceneChanged);
|
||||
Time.sceneChanged();
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,21 @@ abstract class EntityProcessingSystem extends EntitySystem {
|
||||
super(matcher);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理特定的实体
|
||||
* @param entity
|
||||
*/
|
||||
public abstract processEntity(entity: Entity);
|
||||
|
||||
public lateProcessEntity(entity: Entity) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 遍历这个系统的所有实体并逐个处理它们
|
||||
* @param entities
|
||||
*/
|
||||
protected process(entities: Entity[]) {
|
||||
entities.forEach(entity => this.processEntity(entity));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
abstract class PassiveSystem extends EntitySystem {
|
||||
public onChanged(entity: Entity){
|
||||
|
||||
}
|
||||
|
||||
protected process(entities: Entity[]){
|
||||
// 我们用我们自己的不考虑实体的基本实体系统来代替
|
||||
this.begin();
|
||||
this.end();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/** 用于协调其他系统的通用系统基类 */
|
||||
abstract class ProcessingSystem extends EntitySystem {
|
||||
public onChanged(entity: Entity){
|
||||
|
||||
}
|
||||
|
||||
protected process(entities: Entity[]){
|
||||
this.begin();
|
||||
this.processSystem();
|
||||
this.end();
|
||||
}
|
||||
|
||||
/** 处理我们的系统 每帧调用 */
|
||||
public abstract processSystem();
|
||||
}
|
||||
@@ -1,336 +0,0 @@
|
||||
enum DirtyType{
|
||||
clean,
|
||||
positionDirty,
|
||||
scaleDirty,
|
||||
rotationDirty,
|
||||
}
|
||||
|
||||
enum ComponentTransform{
|
||||
position,
|
||||
scale,
|
||||
rotation
|
||||
}
|
||||
|
||||
class Transform {
|
||||
/** 相关联的实体 */
|
||||
public readonly entity: Entity;
|
||||
private _children: Transform[];
|
||||
private _parent: Transform;
|
||||
|
||||
private _localPosition: Vector2;
|
||||
private _localRotation: number = 0;
|
||||
private _localScale: Vector2;
|
||||
|
||||
private _translationMatrix: Matrix2D;
|
||||
private _rotationMatrix: Matrix2D;
|
||||
private _scaleMatrix: Matrix2D;
|
||||
|
||||
private _worldTransform = Matrix2D.identity;
|
||||
private _worldToLocalTransform = Matrix2D.identity;
|
||||
private _worldInverseTransform = Matrix2D.identity;
|
||||
|
||||
private _rotation: number = 0;
|
||||
private _position: Vector2;
|
||||
private _scale: Vector2;
|
||||
|
||||
private _localTransform;
|
||||
private _hierachyDirty: DirtyType;
|
||||
private _localDirty: boolean;
|
||||
private _localPositionDirty: boolean;
|
||||
private _localScaleDirty: boolean;
|
||||
private _localRotationDirty: boolean;
|
||||
private _positionDirty: boolean;
|
||||
private _worldToLocalDirty: boolean;
|
||||
private _worldInverseDirty: boolean;
|
||||
|
||||
public get childCount(){
|
||||
return this._children.length;
|
||||
}
|
||||
|
||||
constructor(entity: Entity){
|
||||
this.entity = entity;
|
||||
this._scale = this._localScale = Vector2.one;
|
||||
this._children = [];
|
||||
}
|
||||
|
||||
public getChild(index: number){
|
||||
return this._children[index];
|
||||
}
|
||||
|
||||
public get worldInverseTransform(){
|
||||
this.updateTransform();
|
||||
if (this._worldInverseDirty){
|
||||
this._worldInverseTransform = Matrix2D.invert(this._worldTransform, this._worldInverseTransform);
|
||||
this._worldInverseDirty = false;
|
||||
}
|
||||
|
||||
return this._worldInverseTransform;
|
||||
}
|
||||
|
||||
public get localToWorldTransform(){
|
||||
this.updateTransform();
|
||||
return this._worldTransform;
|
||||
}
|
||||
|
||||
public get worldToLocalTransform(){
|
||||
if (this._worldToLocalDirty){
|
||||
if (!this.parent){
|
||||
this._worldInverseTransform = Matrix2D.identity;
|
||||
} else{
|
||||
this.parent.updateTransform();
|
||||
this._worldToLocalTransform = Matrix2D.invert(this.parent._worldTransform, this._worldToLocalTransform);
|
||||
}
|
||||
|
||||
this._worldToLocalDirty = false;
|
||||
}
|
||||
|
||||
return this._worldToLocalTransform;
|
||||
}
|
||||
|
||||
public get parent(){
|
||||
return this._parent;
|
||||
}
|
||||
|
||||
public set parent(value: Transform){
|
||||
this.setParent(value);
|
||||
}
|
||||
|
||||
public setParent(parent: Transform){
|
||||
if (this._parent == parent)
|
||||
return this;
|
||||
|
||||
if (this._parent)
|
||||
this._parent._children.remove(this);
|
||||
|
||||
if (parent)
|
||||
parent._children.push(this);
|
||||
|
||||
this._parent = parent;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public get rotation() {
|
||||
this.updateTransform();
|
||||
return this._rotation;
|
||||
}
|
||||
|
||||
public set rotation(value: number){
|
||||
this.setRotation(value);
|
||||
}
|
||||
|
||||
public get localRotation(){
|
||||
this.updateTransform();
|
||||
return this._localRotation;
|
||||
}
|
||||
|
||||
public set localRotation(value: number){
|
||||
this.setLocalRotation(value);
|
||||
}
|
||||
|
||||
public get position(){
|
||||
this.updateTransform();
|
||||
if (!this.parent){
|
||||
this._position = this._localPosition;
|
||||
}else{
|
||||
this.parent.updateTransform();
|
||||
this._position = Vector2.transform(this._localPosition, this.parent._worldTransform);
|
||||
}
|
||||
|
||||
return this._position;
|
||||
}
|
||||
|
||||
public set position(value: Vector2){
|
||||
this.setPosition(value);
|
||||
}
|
||||
|
||||
public get localPosition(){
|
||||
this.updateTransform();
|
||||
return this._localPosition;
|
||||
}
|
||||
|
||||
public set localPosition(value: Vector2){
|
||||
this.setLocalPosition(value);
|
||||
}
|
||||
|
||||
public get scale(){
|
||||
this.updateTransform();
|
||||
return this._scale;
|
||||
}
|
||||
|
||||
public set scale(value: Vector2){
|
||||
this.setScale(value);
|
||||
}
|
||||
|
||||
public get localScale(){
|
||||
this.updateTransform();
|
||||
return this._localScale;
|
||||
}
|
||||
|
||||
public set localScale(value: Vector2){
|
||||
this.setLocalScale(value);
|
||||
}
|
||||
|
||||
public get rotationDegrees(){
|
||||
return MathHelper.toDegrees(this._rotation);
|
||||
}
|
||||
|
||||
public set rotationDegrees(value: number){
|
||||
this.setRotation(MathHelper.toRadians(value));
|
||||
}
|
||||
|
||||
public get localRotationDegrees(){
|
||||
return MathHelper.toDegrees(this._localRotation);
|
||||
}
|
||||
|
||||
public set localRotationDegrees(value: number){
|
||||
this.localRotation = MathHelper.toRadians(value);
|
||||
}
|
||||
|
||||
public setLocalScale(scale: Vector2){
|
||||
this._localScale = scale;
|
||||
this._localDirty = this._positionDirty = this._localScaleDirty = true;
|
||||
this.setDirty(DirtyType.scaleDirty);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public setScale(scale: Vector2){
|
||||
this._scale = scale;
|
||||
if (this.parent){
|
||||
this.localScale = Vector2.divide(scale, this.parent._scale);
|
||||
}else{
|
||||
this.localScale = scale;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public setLocalRotationDegrees(degrees: number){
|
||||
return this.setLocalRotation(MathHelper.toRadians(degrees));
|
||||
}
|
||||
|
||||
public setLocalRotation(radians: number){
|
||||
this._localRotation = radians;
|
||||
this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true;
|
||||
this.setDirty(DirtyType.rotationDirty);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public setRotation(radians: number){
|
||||
this._rotation = radians;
|
||||
if (this.parent){
|
||||
this.localRotation = this.parent.rotation + radians;
|
||||
} else {
|
||||
this.localRotation = radians;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public setRotationDegrees(degrees: number){
|
||||
return this.setRotation(MathHelper.toRadians(degrees));
|
||||
}
|
||||
|
||||
public setLocalPosition(localPosition: Vector2){
|
||||
if (localPosition == this._localPosition)
|
||||
return this;
|
||||
|
||||
this._localPosition = localPosition;
|
||||
this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true;
|
||||
this.setDirty(DirtyType.positionDirty);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public setPosition(position: Vector2){
|
||||
if (position == this._position)
|
||||
return this;
|
||||
|
||||
this._position = position;
|
||||
if (this.parent){
|
||||
this.localPosition = Vector2.transform(this._position, this._worldToLocalTransform);
|
||||
}else{
|
||||
this.localPosition = position;
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public setDirty(dirtyFlagType: DirtyType){
|
||||
if ((this._hierachyDirty & dirtyFlagType) == 0){
|
||||
this._hierachyDirty |= dirtyFlagType;
|
||||
|
||||
switch (dirtyFlagType){
|
||||
case DirtyType.positionDirty:
|
||||
this.entity.onTransformChanged(ComponentTransform.position);
|
||||
break;
|
||||
case DirtyType.rotationDirty:
|
||||
this.entity.onTransformChanged(ComponentTransform.rotation);
|
||||
break;
|
||||
case DirtyType.scaleDirty:
|
||||
this.entity.onTransformChanged(ComponentTransform.scale);
|
||||
break;
|
||||
}
|
||||
|
||||
if (this._children == null)
|
||||
this._children = [];
|
||||
|
||||
for (let i = 0; i < this._children.length; i ++){
|
||||
this._children[i].setDirty(dirtyFlagType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public updateTransform(){
|
||||
if (this._hierachyDirty != DirtyType.clean){
|
||||
if (this.parent)
|
||||
this.parent.updateTransform();
|
||||
|
||||
if (this._localDirty){
|
||||
if (this._localPositionDirty){
|
||||
this._translationMatrix = Matrix2D.createTranslation(this._localPosition.x, this._localPosition.y);
|
||||
this._localPositionDirty = false;
|
||||
}
|
||||
|
||||
if (this._localRotationDirty){
|
||||
this._rotationMatrix = Matrix2D.createRotation(this._localRotation);
|
||||
this._localRotationDirty = false;
|
||||
}
|
||||
|
||||
|
||||
if (this._localScaleDirty){
|
||||
this._scaleMatrix = Matrix2D.createScale(this._localScale.x, this._localScale.y);
|
||||
this._localScaleDirty = false;
|
||||
}
|
||||
|
||||
|
||||
this._localTransform = Matrix2D.multiply(this._scaleMatrix, this._rotationMatrix);
|
||||
this._localTransform = Matrix2D.multiply(this._localTransform, this._translationMatrix);
|
||||
|
||||
if (!this.parent){
|
||||
this._worldTransform = this._localTransform;
|
||||
this._rotation = this._localRotation;
|
||||
this._scale = this._localScale;
|
||||
this._worldInverseDirty = true;
|
||||
}
|
||||
|
||||
this._localDirty = false;
|
||||
}
|
||||
|
||||
if (this.parent){
|
||||
this._worldTransform = Matrix2D.multiply(this._localTransform, this.parent._worldTransform);
|
||||
|
||||
this._rotation = this._localRotation + this.parent._rotation;
|
||||
this._scale = Vector2.multiply( this.parent._scale, this._localScale);
|
||||
this._worldInverseDirty = true;
|
||||
}
|
||||
|
||||
this._worldToLocalDirty = true;
|
||||
this._positionDirty = true;
|
||||
this._hierachyDirty = DirtyType.clean;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,35 @@
|
||||
class ComponentList {
|
||||
private _entity: Entity;
|
||||
/** 添加到实体的组件列表 */
|
||||
private _components: Component[] = [];
|
||||
/** 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工 */
|
||||
private _componentsToAdd: Component[] = [];
|
||||
/** 标记要删除此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工 */
|
||||
private _componentsToRemove: Component[] = [];
|
||||
private _tempBufferList: Component[] = [];
|
||||
|
||||
constructor(entity: Entity){
|
||||
constructor(entity: Entity) {
|
||||
this._entity = entity;
|
||||
}
|
||||
|
||||
public get count(){
|
||||
public get count() {
|
||||
return this._components.length;
|
||||
}
|
||||
|
||||
public get buffer(){
|
||||
public get buffer() {
|
||||
return this._components;
|
||||
}
|
||||
|
||||
public add(component: Component){
|
||||
public add(component: Component) {
|
||||
this._componentsToAdd.push(component);
|
||||
}
|
||||
|
||||
public remove(component: Component){
|
||||
if (this._componentsToAdd.contains(component)){
|
||||
public remove(component: Component) {
|
||||
if (this._componentsToRemove.contains(component))
|
||||
console.warn(`You are trying to remove a Component (${component}) that you already removed`)
|
||||
|
||||
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
|
||||
if (this._componentsToAdd.contains(component)) {
|
||||
this._componentsToAdd.remove(component);
|
||||
return;
|
||||
}
|
||||
@@ -30,8 +37,11 @@ class ComponentList {
|
||||
this._componentsToRemove.push(component);
|
||||
}
|
||||
|
||||
public removeAllComponents(){
|
||||
for (let i = 0; i < this._components.length; i ++){
|
||||
/**
|
||||
* 立即从组件列表中删除所有组件
|
||||
*/
|
||||
public removeAllComponents() {
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
this.handleRemove(this._components[i]);
|
||||
}
|
||||
|
||||
@@ -40,27 +50,37 @@ class ComponentList {
|
||||
this._componentsToRemove.length = 0;
|
||||
}
|
||||
|
||||
public deregisterAllComponents(){
|
||||
for (let i = 0; i < this._components.length; i ++){
|
||||
public deregisterAllComponents() {
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
let component = this._components[i];
|
||||
|
||||
// 处理渲染层列表
|
||||
if (component instanceof RenderableComponent)
|
||||
this._entity.scene.renderableComponents.remove(component);
|
||||
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
|
||||
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
|
||||
}
|
||||
}
|
||||
|
||||
public registerAllComponents(){
|
||||
for (let i = 0; i < this._components.length; i ++){
|
||||
public registerAllComponents() {
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
let component = this._components[i];
|
||||
|
||||
if (component instanceof RenderableComponent)
|
||||
this._entity.scene.renderableComponents.add(component);
|
||||
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
|
||||
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
|
||||
}
|
||||
}
|
||||
|
||||
public updateLists(){
|
||||
if (this._componentsToRemove.length > 0){
|
||||
for (let i = 0; i < this._componentsToRemove.length; i ++){
|
||||
/**
|
||||
* 处理任何需要删除或添加的组件
|
||||
*/
|
||||
public updateLists() {
|
||||
if (this._componentsToRemove.length > 0) {
|
||||
for (let i = 0; i < this._componentsToRemove.length; i++) {
|
||||
this.handleRemove(this._componentsToRemove[i]);
|
||||
this._components.remove(this._componentsToRemove[i]);
|
||||
}
|
||||
@@ -68,9 +88,11 @@ class ComponentList {
|
||||
this._componentsToRemove.length = 0;
|
||||
}
|
||||
|
||||
if (this._componentsToAdd.length > 0){
|
||||
for (let i = 0, count = this._componentsToAdd.length; i < count; i ++){
|
||||
if (this._componentsToAdd.length > 0) {
|
||||
for (let i = 0, count = this._componentsToAdd.length; i < count; i++) {
|
||||
let component = this._componentsToAdd[i];
|
||||
if (component instanceof RenderableComponent)
|
||||
this._entity.scene.renderableComponents.add(component);
|
||||
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
|
||||
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
|
||||
@@ -79,13 +101,16 @@ class ComponentList {
|
||||
this._tempBufferList.push(component);
|
||||
}
|
||||
|
||||
// 在调用onAddedToEntity之前清除,以防添加更多组件
|
||||
this._componentsToAdd.length = 0;
|
||||
|
||||
for (let i = 0; i < this._tempBufferList.length; i++){
|
||||
// 现在所有的组件都添加到了场景中,我们再次循环并调用onAddedToEntity/onEnabled
|
||||
for (let i = 0; i < this._tempBufferList.length; i++) {
|
||||
let component = this._tempBufferList[i];
|
||||
component.onAddedToEntity();
|
||||
|
||||
if (component.enabled){
|
||||
// enabled检查实体和组件
|
||||
if (component.enabled) {
|
||||
component.onEnabled();
|
||||
}
|
||||
}
|
||||
@@ -94,7 +119,22 @@ class ComponentList {
|
||||
}
|
||||
}
|
||||
|
||||
private handleRemove(component: Component){
|
||||
public onEntityTransformChanged(comp: TransformComponent) {
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
if (this._components[i].enabled)
|
||||
this._components[i].onEntityTransformChanged(comp);
|
||||
}
|
||||
|
||||
for (let i = 0; i < this._componentsToAdd.length; i++) {
|
||||
if (this._componentsToAdd[i].enabled)
|
||||
this._componentsToAdd[i].onEntityTransformChanged(comp);
|
||||
}
|
||||
}
|
||||
|
||||
private handleRemove(component: Component) {
|
||||
if (component instanceof RenderableComponent)
|
||||
this._entity.scene.renderableComponents.remove(component);
|
||||
|
||||
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
|
||||
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
|
||||
|
||||
@@ -102,15 +142,23 @@ class ComponentList {
|
||||
component.entity = null;
|
||||
}
|
||||
|
||||
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T{
|
||||
for (let i = 0; i < this._components.length; i ++){
|
||||
/**
|
||||
* 获取类型T的第一个组件并返回它
|
||||
* 可以选择跳过检查未初始化的组件(尚未调用onAddedToEntity方法的组件)
|
||||
* 如果没有找到组件,则返回null。
|
||||
* @param type
|
||||
* @param onlyReturnInitializedComponents
|
||||
*/
|
||||
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T {
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
let component = this._components[i];
|
||||
if (component instanceof type)
|
||||
return component as T;
|
||||
}
|
||||
|
||||
if (!onlyReturnInitializedComponents){
|
||||
for (let i = 0; i < this._componentsToAdd.length; i ++){
|
||||
// 我们可以选择检查挂起的组件,以防addComponent和getComponent在同一个框架中被调用
|
||||
if (!onlyReturnInitializedComponents) {
|
||||
for (let i = 0; i < this._componentsToAdd.length; i++) {
|
||||
let component = this._componentsToAdd[i];
|
||||
if (component instanceof type)
|
||||
return component as T;
|
||||
@@ -120,31 +168,36 @@ class ComponentList {
|
||||
return null;
|
||||
}
|
||||
|
||||
public getComponents(typeName: string | any, components?){
|
||||
/**
|
||||
* 获取T类型的所有组件,但不使用列表分配
|
||||
* @param typeName
|
||||
* @param components
|
||||
*/
|
||||
public getComponents(typeName: string | any, components?) {
|
||||
if (!components)
|
||||
components = [];
|
||||
|
||||
for (let i = 0; i < this._components.length; i ++){
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
let component = this._components[i];
|
||||
if (typeof(typeName) == "string"){
|
||||
if (egret.is(component, typeName)){
|
||||
if (typeof (typeName) == "string") {
|
||||
if (egret.is(component, typeName)) {
|
||||
components.push(component);
|
||||
}
|
||||
}else{
|
||||
if (component instanceof typeName){
|
||||
} else {
|
||||
if (component instanceof typeName) {
|
||||
components.push(component);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < this._componentsToAdd.length; i ++){
|
||||
for (let i = 0; i < this._componentsToAdd.length; i++) {
|
||||
let component = this._componentsToAdd[i];
|
||||
if (typeof(typeName) == "string"){
|
||||
if (egret.is(component, typeName)){
|
||||
if (typeof (typeName) == "string") {
|
||||
if (egret.is(component, typeName)) {
|
||||
components.push(component);
|
||||
}
|
||||
}else{
|
||||
if (component instanceof typeName){
|
||||
} else {
|
||||
if (component instanceof typeName) {
|
||||
components.push(component);
|
||||
}
|
||||
}
|
||||
@@ -153,24 +206,19 @@ class ComponentList {
|
||||
return components;
|
||||
}
|
||||
|
||||
public update(){
|
||||
public update() {
|
||||
this.updateLists();
|
||||
for (let i = 0; i < this._components.length; i ++){
|
||||
let component = this._components[i];
|
||||
if (component.enabled && (component.updateInterval == 1 || Time.frameCount % component.updateInterval == 0))
|
||||
component.update();
|
||||
}
|
||||
}
|
||||
for (let i = 0; i < this._components.length; i++) {
|
||||
let updatable = this._components[i];
|
||||
let updateableComponent;
|
||||
if (updatable instanceof Component)
|
||||
updateableComponent = updatable as Component;
|
||||
|
||||
public onEntityTransformChanged(comp){
|
||||
for (let i = 0; i < this._components.length; i++){
|
||||
if (this._components[i].enabled)
|
||||
this._components[i].onEntityTransformChanged(comp);
|
||||
}
|
||||
|
||||
for (let i = 0; i < this._componentsToAdd.length; i ++){
|
||||
if (this._componentsToAdd[i].enabled)
|
||||
this._componentsToAdd[i].onEntityTransformChanged(comp);
|
||||
if (updatable.enabled &&
|
||||
updateableComponent.enabled &&
|
||||
(updateableComponent.updateInterval == 1 ||
|
||||
Time.frameCount % updateableComponent.updateInterval == 0))
|
||||
updatable.update();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,6 +82,8 @@ class EntityList{
|
||||
this.updateLists();
|
||||
|
||||
for (let i = 0; i < this._entities.length; i ++){
|
||||
this._entities[i]._isDestoryed = true;
|
||||
this._entities[i].onRemovedFromScene();
|
||||
this._entities[i].scene = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,18 @@ class Matcher{
|
||||
return new Matcher();
|
||||
}
|
||||
|
||||
public getAllSet(){
|
||||
return this.allSet;
|
||||
}
|
||||
|
||||
public getExclusionSet(){
|
||||
return this.exclusionSet;
|
||||
}
|
||||
|
||||
public getOneSet(){
|
||||
return this.oneSet;
|
||||
}
|
||||
|
||||
public IsIntersted(e: Entity){
|
||||
if (!this.allSet.isEmpty()){
|
||||
for (let i = this.allSet.nextSetBit(0); i >= 0; i = this.allSet.nextSetBit(i + 1)){
|
||||
@@ -23,4 +35,28 @@ class Matcher{
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public all(...types: any[]): Matcher{
|
||||
types.forEach(type => {
|
||||
this.allSet.set(ComponentTypeManager.getIndexFor(type));
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public exclude(...types: any[]){
|
||||
types.forEach(type => {
|
||||
this.exclusionSet.set(ComponentTypeManager.getIndexFor(type));
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public one(...types: any[]){
|
||||
types.forEach(type => {
|
||||
this.oneSet.set(ComponentTypeManager.getIndexFor(type));
|
||||
});
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
class ObjectUtils {
|
||||
/**
|
||||
* 对象深度拷贝
|
||||
* @param p any 源对象
|
||||
* @param c any 目标对象, 不传则返回新对象, 传则合并属性, 相同名字的属性则会覆盖
|
||||
*/
|
||||
public static clone<T>(p: any, c: T = null): T {
|
||||
var c = c || <T>{};
|
||||
for (let i in p) {
|
||||
if (typeof p[i] === 'object') {
|
||||
c[i] = p[i] instanceof Array ? [] : {};
|
||||
this.clone(p[i], c[i]);
|
||||
}
|
||||
else {
|
||||
c[i] = p[i];
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
class RenderableComponentList {
|
||||
private _components: IRenderable[] = [];
|
||||
public get count(){
|
||||
return this._components.length;
|
||||
}
|
||||
|
||||
public get buffer(){
|
||||
return this._components;
|
||||
}
|
||||
|
||||
public add(component: IRenderable){
|
||||
this._components.push(component);
|
||||
}
|
||||
|
||||
public remove(component: IRenderable){
|
||||
this._components.remove(component);
|
||||
}
|
||||
|
||||
public updateList(){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
class StringUtils {
|
||||
/**
|
||||
* 匹配中文字符
|
||||
* @param str 需要匹配的字符串
|
||||
* @return
|
||||
*/
|
||||
public static matchChineseWord(str: string): string[] {
|
||||
//中文字符的unicode值[\u4E00-\u9FA5]
|
||||
let patternA: RegExp = /[\u4E00-\u9FA5]+/gim;
|
||||
return str.match(patternA);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除字符串左端的空白字符
|
||||
* @param target 目标字符串
|
||||
* @return
|
||||
*/
|
||||
public static lTrim(target: string): string {
|
||||
let startIndex: number = 0;
|
||||
while (this.isWhiteSpace(target.charAt(startIndex))) {
|
||||
startIndex++;
|
||||
}
|
||||
return target.slice(startIndex, target.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 去除字符串右端的空白字符
|
||||
* @param target 目标字符串
|
||||
* @return
|
||||
*/
|
||||
public static rTrim(target: string): string {
|
||||
let endIndex: number = target.length - 1;
|
||||
while (this.isWhiteSpace(target.charAt(endIndex))) {
|
||||
endIndex--;
|
||||
}
|
||||
return target.slice(0, endIndex + 1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 返回一个去除2段空白字符的字符串
|
||||
* @param target
|
||||
* @return 返回一个去除2段空白字符的字符串
|
||||
*/
|
||||
public static trim(target: string): string {
|
||||
if (target == null) {
|
||||
return null;
|
||||
}
|
||||
return this.rTrim(this.lTrim(target));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回该字符是否为空白字符
|
||||
* @param str
|
||||
* @return 返回该字符是否为空白字符
|
||||
*/
|
||||
public static isWhiteSpace(str: string): boolean {
|
||||
if (str == " " || str == "\t" || str == "\r" || str == "\n")
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回执行替换后的字符串
|
||||
* @param mainStr 待查找字符串
|
||||
* @param targetStr 目标字符串
|
||||
* @param replaceStr 替换字符串
|
||||
* @param caseMark 是否忽略大小写
|
||||
* @return 返回执行替换后的字符串
|
||||
*/
|
||||
public static replaceMatch(mainStr: string, targetStr: string,
|
||||
replaceStr: string, caseMark: boolean = false): string {
|
||||
let len: number = mainStr.length;
|
||||
let tempStr: string = "";
|
||||
let isMatch: boolean = false;
|
||||
let tempTarget: string = caseMark == true ? targetStr.toLowerCase() : targetStr;
|
||||
for (let i: number = 0; i < len; i++) {
|
||||
isMatch = false;
|
||||
if (mainStr.charAt(i) == tempTarget.charAt(0)) {
|
||||
if (mainStr.substr(i, tempTarget.length) == tempTarget) {
|
||||
isMatch = true;
|
||||
}
|
||||
}
|
||||
if (isMatch) {
|
||||
tempStr += replaceStr;
|
||||
i = i + tempTarget.length - 1;
|
||||
}
|
||||
else {
|
||||
tempStr += mainStr.charAt(i);
|
||||
}
|
||||
}
|
||||
return tempStr;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 特殊符号字符串
|
||||
*/
|
||||
private static specialSigns: string[] = [
|
||||
'&', '&',
|
||||
'<', '<',
|
||||
'>', '>',
|
||||
'"', '"',
|
||||
"'", ''',
|
||||
'®', '®',
|
||||
'©', '©',
|
||||
'™', '™',
|
||||
];
|
||||
|
||||
|
||||
/**
|
||||
* 用html实体换掉字符窜中的特殊字符
|
||||
* @param str 需要替换的字符串
|
||||
* @param reversion 是否翻转替换:将转义符号替换为正常的符号
|
||||
* @return 换掉特殊字符后的字符串
|
||||
*/
|
||||
public static htmlSpecialChars(str: string, reversion: boolean = false): string {
|
||||
let len: number = this.specialSigns.length;
|
||||
for (let i: number = 0; i < len; i += 2) {
|
||||
let from: string;
|
||||
let to: string;
|
||||
from = this.specialSigns[i];
|
||||
to = this.specialSigns[i + 1];
|
||||
if (reversion) {
|
||||
let temp: string = from;
|
||||
from = to;
|
||||
to = temp;
|
||||
}
|
||||
str = this.replaceMatch(str, from, to);
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 给数字字符前面添 "0"
|
||||
*
|
||||
* <pre>
|
||||
*
|
||||
* trace( StringFormat.zfill('1') );
|
||||
* // 01
|
||||
*
|
||||
* trace( StringFormat.zfill('16', 5) );
|
||||
* // 00016
|
||||
*
|
||||
* trace( StringFormat.zfill('-3', 3) );
|
||||
* // -03
|
||||
*
|
||||
* </pre>
|
||||
*
|
||||
* @param str 要进行处理的字符串
|
||||
* @param width 处理后字符串的长度,
|
||||
* 如果str.length >= width,将不做任何处理直接返回原始的str。
|
||||
* @return
|
||||
*
|
||||
*/
|
||||
public static zfill(str: string, width: number = 2): string {
|
||||
if (!str) {
|
||||
return str;
|
||||
}
|
||||
width = Math.floor(width);
|
||||
let slen: number = str.length;
|
||||
if (slen >= width) {
|
||||
return str;
|
||||
}
|
||||
|
||||
let negative: boolean = false;
|
||||
if (str.substr(0, 1) == '-') {
|
||||
negative = true;
|
||||
str = str.substr(1);
|
||||
}
|
||||
|
||||
let len: number = width - slen;
|
||||
for (let i: number = 0; i < len; i++) {
|
||||
str = '0' + str;
|
||||
}
|
||||
|
||||
if (negative) {
|
||||
str = '-' + str;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 翻转字符串
|
||||
* @param str 字符串
|
||||
* @return 翻转后的字符串
|
||||
*/
|
||||
public static reverse(str: string): string {
|
||||
if (str.length > 1)
|
||||
return this.reverse(str.substring(1)) + str.substring(0, 1);
|
||||
else
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 截断某段字符串
|
||||
* @param str 目标字符串
|
||||
* @param start 需要截断的起始索引
|
||||
* @param len 截断长度
|
||||
* @param order 顺序,true从字符串头部开始计算,false从字符串尾巴开始结算。
|
||||
* @return 截断后的字符串
|
||||
*/
|
||||
public static cutOff(str: string, start: number,
|
||||
len: number, order: boolean = true): string {
|
||||
start = Math.floor(start);
|
||||
len = Math.floor(len);
|
||||
let length: number = str.length;
|
||||
if (start > length) start = length;
|
||||
let s: number = start;
|
||||
let e: number = start + len;
|
||||
let newStr: string;
|
||||
if (order) {
|
||||
newStr = str.substring(0, s) + str.substr(e, length);
|
||||
}
|
||||
else {
|
||||
s = length - 1 - start - len;
|
||||
e = s + len;
|
||||
newStr = str.substring(0, s + 1) + str.substr(e + 1, length);
|
||||
}
|
||||
return newStr;
|
||||
}
|
||||
|
||||
/**{0} 字符替换 */
|
||||
public static strReplace(str: string, rStr: string[]): string {
|
||||
let i: number = 0, len: number = rStr.length;
|
||||
for (; i < len; i++) {
|
||||
if (rStr[i] == null || rStr[i] == "") {
|
||||
rStr[i] = "无";
|
||||
}
|
||||
str = str.replace("{" + i + "}", rStr[i]);
|
||||
}
|
||||
return str
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
/**
|
||||
* 纹理帮助类
|
||||
*/
|
||||
class TextureUtils {
|
||||
public static sharedCanvas: HTMLCanvasElement;
|
||||
public static sharedContext: CanvasRenderingContext2D;
|
||||
|
||||
public static convertImageToCanvas(texture: egret.Texture, rect?: egret.Rectangle): HTMLCanvasElement{
|
||||
if (!this.sharedCanvas){
|
||||
this.sharedCanvas = egret.sys.createCanvas();
|
||||
this.sharedContext = this.sharedCanvas.getContext("2d");
|
||||
}
|
||||
|
||||
let w = texture.$getTextureWidth();
|
||||
let h = texture.$getTextureHeight();
|
||||
if (!rect){
|
||||
rect = egret.$TempRectangle;
|
||||
rect.x = 0;
|
||||
rect.y = 0;
|
||||
rect.width = w;
|
||||
rect.height = h;
|
||||
}
|
||||
|
||||
rect.x = Math.min(rect.x, w - 1);
|
||||
rect.y = Math.min(rect.y, h - 1);
|
||||
rect.width = Math.min(rect.width, w - rect.x);
|
||||
rect.height = Math.min(rect.height, h - rect.y);
|
||||
|
||||
let iWidth = Math.floor(rect.width);
|
||||
let iHeight = Math.floor(rect.height);
|
||||
let surface = this.sharedCanvas;
|
||||
surface["style"]["width"] = iWidth + "px";
|
||||
surface["style"]["height"] = iHeight + "px";
|
||||
this.sharedCanvas.width = iWidth;
|
||||
this.sharedCanvas.height = iHeight;
|
||||
|
||||
if (egret.Capabilities.renderMode == "webgl") {
|
||||
let renderTexture: egret.RenderTexture;
|
||||
//webgl下非RenderTexture纹理先画到RenderTexture
|
||||
if (!(<egret.RenderTexture>texture).$renderBuffer) {
|
||||
if (egret.sys.systemRenderer["renderClear"]) {
|
||||
egret.sys.systemRenderer["renderClear"]();
|
||||
}
|
||||
renderTexture = new egret.RenderTexture();
|
||||
renderTexture.drawToTexture(new egret.Bitmap(texture));
|
||||
}
|
||||
else {
|
||||
renderTexture = <egret.RenderTexture>texture;
|
||||
}
|
||||
//从RenderTexture中读取像素数据,填入canvas
|
||||
let pixels = renderTexture.$renderBuffer.getPixels(rect.x, rect.y, iWidth, iHeight);
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
for (let i = 0; i < pixels.length; i += 4) {
|
||||
this.sharedContext.fillStyle =
|
||||
'rgba(' + pixels[i]
|
||||
+ ',' + pixels[i + 1]
|
||||
+ ',' + pixels[i + 2]
|
||||
+ ',' + (pixels[i + 3] / 255) + ')';
|
||||
this.sharedContext.fillRect(x, y, 1, 1);
|
||||
x++;
|
||||
if (x == iWidth) {
|
||||
x = 0;
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!(<egret.RenderTexture>texture).$renderBuffer) {
|
||||
renderTexture.dispose();
|
||||
}
|
||||
|
||||
return surface;
|
||||
}
|
||||
else {
|
||||
let bitmapData = texture;
|
||||
let offsetX: number = Math.round(bitmapData.$offsetX);
|
||||
let offsetY: number = Math.round(bitmapData.$offsetY);
|
||||
let bitmapWidth: number = bitmapData.$bitmapWidth;
|
||||
let bitmapHeight: number = bitmapData.$bitmapHeight;
|
||||
let $TextureScaleFactor = SceneManager.stage.textureScaleFactor;
|
||||
this.sharedContext.drawImage(bitmapData.$bitmapData.source, bitmapData.$bitmapX + rect.x / $TextureScaleFactor, bitmapData.$bitmapY + rect.y / $TextureScaleFactor,
|
||||
bitmapWidth * rect.width / w, bitmapHeight * rect.height / h, offsetX, offsetY, rect.width, rect.height);
|
||||
return surface;
|
||||
}
|
||||
}
|
||||
|
||||
public static toDataURL(type: string, texture: egret.Texture, rect?: egret.Rectangle, encoderOptions?): string {
|
||||
try {
|
||||
let surface = this.convertImageToCanvas(texture, rect);
|
||||
let result = surface.toDataURL(type, encoderOptions);
|
||||
return result;
|
||||
}
|
||||
catch (e) {
|
||||
egret.$error(1033);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 有些杀毒软件认为 saveToFile 可能是一个病毒文件
|
||||
* @param type
|
||||
* @param texture
|
||||
* @param filePath
|
||||
* @param rect
|
||||
* @param encoderOptions
|
||||
*/
|
||||
public static eliFoTevas(type: string, texture: egret.Texture, filePath: string, rect?: egret.Rectangle, encoderOptions?): void {
|
||||
let surface = this.convertImageToCanvas(texture, rect);
|
||||
let result = (surface as any).toTempFilePathSync({
|
||||
fileType: type.indexOf("png") >= 0 ? "png" : "jpg"
|
||||
});
|
||||
|
||||
wx.getFileSystemManager().saveFile({
|
||||
tempFilePath: result,
|
||||
filePath: `${wx.env.USER_DATA_PATH}/${filePath}`,
|
||||
success: function (res) {
|
||||
//todo
|
||||
}
|
||||
})
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static getPixel32(texture: egret.Texture, x: number, y: number): number[] {
|
||||
egret.$warn(1041, "getPixel32", "getPixels");
|
||||
return texture.getPixels(x, y);
|
||||
}
|
||||
|
||||
public static getPixels(texture: egret.Texture, x: number, y: number, width: number = 1, height: number = 1): number[] {
|
||||
//webgl环境下不需要转换成canvas获取像素信息
|
||||
if (egret.Capabilities.renderMode == "webgl") {
|
||||
let renderTexture: egret.RenderTexture;
|
||||
//webgl下非RenderTexture纹理先画到RenderTexture
|
||||
if (!(<egret.RenderTexture>texture).$renderBuffer) {
|
||||
renderTexture = new egret.RenderTexture();
|
||||
renderTexture.drawToTexture(new egret.Bitmap(texture));
|
||||
}
|
||||
else {
|
||||
renderTexture = <egret.RenderTexture>texture;
|
||||
}
|
||||
//从RenderTexture中读取像素数据
|
||||
let pixels = renderTexture.$renderBuffer.getPixels(x, y, width, height);
|
||||
return pixels;
|
||||
}
|
||||
try {
|
||||
let surface = this.convertImageToCanvas(texture);
|
||||
let result = this.sharedContext.getImageData(x, y, width, height).data;
|
||||
return <number[]><any>result;
|
||||
}
|
||||
catch (e) {
|
||||
egret.$error(1039);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,38 @@
|
||||
/** 提供帧定时信息 */
|
||||
class Time {
|
||||
/** deltaTime的未缩放版本。不受时间尺度的影响 */
|
||||
public static unscaledDeltaTime;
|
||||
/** 前一帧到当前帧的时间增量,按时间刻度进行缩放 */
|
||||
public static deltaTime: number = 0;
|
||||
/** 时间刻度缩放 */
|
||||
public static timeScale = 1;
|
||||
public static frameCount = 0;;
|
||||
/** 已传递的帧总数 */
|
||||
public static frameCount = 0;
|
||||
|
||||
private static _lastTime = 0;
|
||||
/** 自场景加载以来的总时间 */
|
||||
public static _timeSinceSceneLoad;
|
||||
|
||||
public static update(currentTime: number){
|
||||
let dt = (currentTime - this._lastTime) / 1000;
|
||||
this.deltaTime = dt * this.timeScale;
|
||||
this.unscaledDeltaTime = dt;
|
||||
this._timeSinceSceneLoad += dt;
|
||||
this.frameCount ++;
|
||||
|
||||
this._lastTime = currentTime;
|
||||
}
|
||||
|
||||
public static sceneChanged(){
|
||||
this._timeSinceSceneLoad = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 允许在间隔检查。只应该使用高于delta的间隔值,否则它将始终返回true。
|
||||
* @param interval
|
||||
*/
|
||||
public static checkEvery(interval: number){
|
||||
// 我们减去了delta,因为timeSinceSceneLoad已经包含了这个update ticks delta
|
||||
return (this._timeSinceSceneLoad / interval) > ((this._timeSinceSceneLoad - this.deltaTime) / interval);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
class TimeUtils {
|
||||
/**
|
||||
* 计算月份ID
|
||||
* @param d 指定计算日期
|
||||
* @returns 月ID
|
||||
*/
|
||||
public static monthId(d: Date = null): number {
|
||||
d = d ? d : new Date();
|
||||
let y = d.getFullYear();
|
||||
let m = d.getMonth() + 1;
|
||||
let g = m < 10 ? "0" : "";
|
||||
return parseInt(y + g + m);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算日期ID
|
||||
* @param d 指定计算日期
|
||||
* @returns 日期ID
|
||||
*/
|
||||
public static dateId(t: Date = null): number {
|
||||
t = t ? t : new Date();
|
||||
let m: number = t.getMonth() + 1;
|
||||
let a = m < 10 ? "0" : "";
|
||||
let d: number = t.getDate();
|
||||
let b = d < 10 ? "0" : "";
|
||||
return parseInt(t.getFullYear() + a + m + b + d);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算周ID
|
||||
* @param d 指定计算日期
|
||||
* @returns 周ID
|
||||
*/
|
||||
public static weekId(d: Date = null, first: boolean = true): number {
|
||||
d = d ? d : new Date();
|
||||
let c: Date = new Date();
|
||||
c.setTime(d.getTime());
|
||||
c.setDate(1); c.setMonth(0);//当年第一天
|
||||
|
||||
let year: number = c.getFullYear();
|
||||
let firstDay: number = c.getDay();
|
||||
if (firstDay == 0) {
|
||||
firstDay = 7;
|
||||
}
|
||||
let max: boolean = false;
|
||||
if (firstDay <= 4) {
|
||||
max = firstDay > 1;
|
||||
c.setDate(c.getDate() - (firstDay - 1));
|
||||
} else {
|
||||
c.setDate(c.getDate() + 7 - firstDay + 1);
|
||||
}
|
||||
let num: number = this.diffDay(d, c, false);
|
||||
if (num < 0) {
|
||||
c.setDate(1); c.setMonth(0);//当年第一天
|
||||
c.setDate(c.getDate() - 1);
|
||||
return this.weekId(c, false);
|
||||
}
|
||||
let week: number = num / 7;
|
||||
let weekIdx: number = Math.floor(week) + 1;
|
||||
if (weekIdx == 53) {
|
||||
c.setTime(d.getTime());
|
||||
c.setDate(c.getDate() - 1);
|
||||
let endDay: number = c.getDay();
|
||||
if (endDay == 0) {
|
||||
endDay = 7;
|
||||
}
|
||||
if (first && (!max || endDay < 4)) {
|
||||
c.setFullYear(c.getFullYear() + 1);
|
||||
c.setDate(1); c.setMonth(0);//当年第一天
|
||||
return this.weekId(c, false);
|
||||
}
|
||||
}
|
||||
let g: string = weekIdx > 9 ? "" : "0";
|
||||
let s: string = year + "00" + g + weekIdx;//加上00防止和月份ID冲突
|
||||
return parseInt(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算俩日期时间差,如果a比b小,返回负数
|
||||
*/
|
||||
public static diffDay(a: Date, b: Date, fixOne: boolean = false): number {
|
||||
let x = (a.getTime() - b.getTime()) / 86400000;
|
||||
return fixOne ? Math.ceil(x) : Math.floor(x);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取本周一 凌晨时间
|
||||
*/
|
||||
public static getFirstDayOfWeek(d?: Date): Date {
|
||||
d = d ? d : new Date();
|
||||
let day = d.getDay() || 7;
|
||||
return new Date(d.getFullYear(), d.getMonth(), d.getDate() + 1 - day, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当日凌晨时间
|
||||
*/
|
||||
public static getFirstOfDay(d?: Date): Date {
|
||||
d = d ? d : new Date();
|
||||
d.setHours(0, 0, 0, 0);
|
||||
return d;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取次日凌晨时间
|
||||
*/
|
||||
public static getNextFirstOfDay(d?: Date): Date {
|
||||
return new Date(this.getFirstOfDay(d).getTime() + 86400000);
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns 2018-12-12
|
||||
*/
|
||||
public static formatDate(date: Date): string {
|
||||
let y = date.getFullYear();
|
||||
let m: any = date.getMonth() + 1;
|
||||
m = m < 10 ? '0' + m : m;
|
||||
let d: any = date.getDate();
|
||||
d = d < 10 ? ('0' + d) : d;
|
||||
return y + '-' + m + '-' + d;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns 2018-12-12 12:12:12
|
||||
*/
|
||||
public static formatDateTime(date: Date): string {
|
||||
let y = date.getFullYear();
|
||||
let m: any = date.getMonth() + 1;
|
||||
m = m < 10 ? ('0' + m) : m;
|
||||
let d: any = date.getDate();
|
||||
d = d < 10 ? ('0' + d) : d;
|
||||
let h = date.getHours();
|
||||
let i: any = date.getMinutes();
|
||||
i = i < 10 ? ('0' + i) : i;
|
||||
let s: any = date.getSeconds();
|
||||
s = s < 10 ? ('0' + s) : s;
|
||||
return y + '-' + m + '-' + d + ' ' + h + ':' + i + ":" + s;
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns s 2018-12-12 或者 2018-12-12 12:12:12
|
||||
*/
|
||||
public static parseDate(s: string): Date {
|
||||
let t = Date.parse(s);
|
||||
if (!isNaN(t)) {
|
||||
return new Date(Date.parse(s.replace(/-/g, "/")));
|
||||
} else {
|
||||
return new Date();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 秒数转换为时间形式。
|
||||
* @param time 秒数
|
||||
* @param partition 分隔符
|
||||
* @param showHour 是否显示小时
|
||||
* @return 返回一个以分隔符分割的时, 分, 秒
|
||||
*
|
||||
* 比如: time = 4351; secondToTime(time)返回字符串01:12:31;
|
||||
*/
|
||||
public static secondToTime(time: number = 0, partition: string = ":", showHour: boolean = true): string {
|
||||
let hours: number = Math.floor(time / 3600);
|
||||
let minutes: number = Math.floor(time % 3600 / 60);
|
||||
let seconds: number = Math.floor(time % 3600 % 60);
|
||||
|
||||
let h: string = hours.toString();
|
||||
let m: string = minutes.toString();
|
||||
let s: string = seconds.toString();
|
||||
|
||||
if (hours < 10) h = "0" + h;
|
||||
if (minutes < 10) m = "0" + m;
|
||||
if (seconds < 10) s = "0" + s;
|
||||
|
||||
let timeStr: string;
|
||||
if (showHour)
|
||||
timeStr = h + partition + m + partition + s;
|
||||
else
|
||||
timeStr = m + partition + s;
|
||||
|
||||
return timeStr;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 时间形式转换为毫秒数。
|
||||
* @param time 以指定分隔符分割的时间字符串
|
||||
* @param partition 分隔符
|
||||
* @return 毫秒数显示的字符串
|
||||
* @throws Error Exception
|
||||
*
|
||||
* 用法1 trace(MillisecondTransform.timeToMillisecond("00:60:00"))
|
||||
* 输出 3600000
|
||||
*
|
||||
*
|
||||
* 用法2 trace(MillisecondTransform.timeToMillisecond("00.60.00","."))
|
||||
* 输出 3600000
|
||||
*/
|
||||
public static timeToMillisecond(time: string, partition: string = ":"): string {
|
||||
let _ary: any[] = time.split(partition);
|
||||
let timeNum: number = 0;
|
||||
let len: number = _ary.length;
|
||||
for (let i: number = 0; i < len; i++) {
|
||||
let n: number = <number>_ary[i];
|
||||
timeNum += n * Math.pow(60, (len - 1 - i));
|
||||
}
|
||||
timeNum *= 1000;
|
||||
return timeNum.toString();
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ declare interface Array<T> {
|
||||
* 移除数组元素
|
||||
* @param element 数组元素
|
||||
*/
|
||||
remove(element): boolean;
|
||||
remove(element: T): boolean;
|
||||
/**
|
||||
* 移除特定索引数组元素
|
||||
* @param index 索引
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
class GaussianBlurEffect extends egret.CustomFilter {
|
||||
// private static blur_frag = "precision mediump float;\n" +
|
||||
// "uniform vec2 blur;\n" +
|
||||
// "uniform sampler2D uSampler;\n" +
|
||||
// "varying vec2 vTextureCoord;\n" +
|
||||
// "uniform vec2 uTextureSize;\n" +
|
||||
// "void main()\n" +
|
||||
// "{\n " +
|
||||
// "const int sampleRadius = 5;\n" +
|
||||
// "const int samples = sampleRadius * 2 + 1;\n" +
|
||||
// "vec2 blurUv = blur / uTextureSize;\n" +
|
||||
// "vec4 color = vec4(0, 0, 0, 0);\n" +
|
||||
// "vec2 uv = vec2(0.0, 0.0);\n" +
|
||||
// "blurUv /= float(sampleRadius);\n" +
|
||||
|
||||
// "for (int i = -sampleRadius; i <= sampleRadius; i++) {\n" +
|
||||
// "uv.x = vTextureCoord.x + float(i) * blurUv.x;\n" +
|
||||
// "uv.y = vTextureCoord.y + float(i) * blurUv.y;\n" +
|
||||
// "color += texture2D(uSampler, uv);\n" +
|
||||
// "}\n" +
|
||||
|
||||
// "color /= float(samples);\n" +
|
||||
// "gl_FragColor = color;\n" +
|
||||
// "}";
|
||||
|
||||
private static blur_frag = "precision mediump float;\n" +
|
||||
"uniform sampler2D uSampler;\n" +
|
||||
"uniform float screenWidth;\n" +
|
||||
"uniform float screenHeight;\n" +
|
||||
|
||||
"float normpdf(in float x, in float sigma)\n" +
|
||||
"{\n" +
|
||||
"return 0.39894*exp(-0.5*x*x/(sigma*sigma))/sigma;\n" +
|
||||
"}\n" +
|
||||
|
||||
"void main()\n" +
|
||||
"{\n" +
|
||||
"vec3 c = texture2D(uSampler, gl_FragCoord.xy / vec2(screenWidth, screenHeight).xy).rgb;\n" +
|
||||
|
||||
"const int mSize = 11;\n" +
|
||||
"const int kSize = (mSize - 1)/2;\n" +
|
||||
"float kernel[mSize];\n" +
|
||||
"vec3 final_colour = vec3(0.0);\n" +
|
||||
|
||||
"float sigma = 7.0;\n" +
|
||||
"float z = 0.0;\n" +
|
||||
"for (int j = 0; j <= kSize; ++j)\n" +
|
||||
"{\n" +
|
||||
"kernel[kSize+j] = kernel[kSize-j] = normpdf(float(j),sigma);\n" +
|
||||
"}\n" +
|
||||
|
||||
"for (int j = 0; j < mSize; ++j)\n" +
|
||||
"{\n" +
|
||||
"z += kernel[j];\n" +
|
||||
"}\n" +
|
||||
|
||||
"for (int i = -kSize; i <= kSize; ++i)\n" +
|
||||
"{\n" +
|
||||
"for (int j = -kSize; j <= kSize; ++j)\n" +
|
||||
"{\n" +
|
||||
"final_colour += kernel[kSize+j]*kernel[kSize+i]*texture2D(uSampler, (gl_FragCoord.xy+vec2(float(i),float(j))) / vec2(screenWidth, screenHeight).xy).rgb;\n" +
|
||||
"}\n}\n" +
|
||||
"gl_FragColor = vec4(final_colour/(z*z), 1.0);\n" +
|
||||
"}";
|
||||
|
||||
constructor(){
|
||||
super(PostProcessor.default_vert, GaussianBlurEffect.blur_frag,{
|
||||
screenWidth: SceneManager.stage.stageWidth,
|
||||
screenHeight: SceneManager.stage.stageHeight
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
class PolygonLightEffect extends egret.CustomFilter {
|
||||
private static vertSrc = "attribute vec2 aVertexPosition;\n" +
|
||||
"attribute vec2 aTextureCoord;\n" +
|
||||
|
||||
"uniform vec2 projectionVector;\n" +
|
||||
|
||||
"varying vec2 vTextureCoord;\n" +
|
||||
|
||||
"const vec2 center = vec2(-1.0, 1.0);\n" +
|
||||
|
||||
"void main(void) {\n" +
|
||||
" gl_Position = vec4( (aVertexPosition / projectionVector) + center , 0.0, 1.0);\n" +
|
||||
" vTextureCoord = aTextureCoord;\n" +
|
||||
"}";
|
||||
private static fragmentSrc = "precision lowp float;\n" +
|
||||
"varying vec2 vTextureCoord;\n" +
|
||||
"uniform sampler2D uSampler;\n" +
|
||||
|
||||
"#define SAMPLE_COUNT 15\n" +
|
||||
|
||||
"uniform vec2 _sampleOffsets[SAMPLE_COUNT];\n" +
|
||||
"uniform float _sampleWeights[SAMPLE_COUNT];\n" +
|
||||
|
||||
"void main(void) {\n" +
|
||||
"vec4 c = vec4(0, 0, 0, 0);\n" +
|
||||
"for( int i = 0; i < SAMPLE_COUNT; i++ )\n" +
|
||||
" c += texture2D( uSampler, vTextureCoord + _sampleOffsets[i] ) * _sampleWeights[i];\n" +
|
||||
"gl_FragColor = c;\n" +
|
||||
"}";
|
||||
|
||||
constructor(){
|
||||
super(PolygonLightEffect.vertSrc, PolygonLightEffect.fragmentSrc);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
class GraphicsCapabilities extends egret.Capabilities {
|
||||
|
||||
public initialize(device: GraphicsDevice){
|
||||
this.platformInitialize(device);
|
||||
}
|
||||
|
||||
private platformInitialize(device: GraphicsDevice){
|
||||
let capabilities = this;
|
||||
capabilities["isMobile"] = true;
|
||||
|
||||
let systemInfo = wx.getSystemInfoSync();
|
||||
let systemStr = systemInfo.system.toLowerCase();
|
||||
if (systemStr.indexOf("ios") > -1){
|
||||
capabilities["os"] = "iOS";
|
||||
} else if(systemStr.indexOf("android") > -1){
|
||||
capabilities["os"] = "Android";
|
||||
}
|
||||
|
||||
let language = systemInfo.language;
|
||||
if (language.indexOf('zh') > -1){
|
||||
language = "zh-CN";
|
||||
} else {
|
||||
language = "en-US";
|
||||
}
|
||||
capabilities["language"] = language;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
class GraphicsDevice {
|
||||
private viewport: Viewport;
|
||||
|
||||
public graphicsCapabilities: GraphicsCapabilities;
|
||||
|
||||
constructor(){
|
||||
this.graphicsCapabilities = new GraphicsCapabilities();
|
||||
this.graphicsCapabilities.initialize(this);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
class PostProcessor {
|
||||
public enable: boolean;
|
||||
public effect: egret.Filter;
|
||||
public scene: Scene;
|
||||
public shape: egret.Shape;
|
||||
|
||||
public static default_vert = "attribute vec2 aVertexPosition;\n" +
|
||||
"attribute vec2 aTextureCoord;\n" +
|
||||
"attribute vec2 aColor;\n" +
|
||||
|
||||
"uniform vec2 projectionVector;\n" +
|
||||
//"uniform vec2 offsetVector;\n" +
|
||||
|
||||
"varying vec2 vTextureCoord;\n" +
|
||||
"varying vec4 vColor;\n" +
|
||||
|
||||
"const vec2 center = vec2(-1.0, 1.0);\n" +
|
||||
|
||||
"void main(void) {\n" +
|
||||
"gl_Position = vec4( (aVertexPosition / projectionVector) + center , 0.0, 1.0);\n" +
|
||||
"vTextureCoord = aTextureCoord;\n" +
|
||||
"vColor = vec4(aColor.x, aColor.x, aColor.x, aColor.x);\n" +
|
||||
"}";
|
||||
|
||||
constructor(effect: egret.Filter = null){
|
||||
this.enable = true;
|
||||
this.effect = effect;
|
||||
}
|
||||
|
||||
public onAddedToScene(scene: Scene){
|
||||
this.scene = scene;
|
||||
this.shape = new egret.Shape();
|
||||
this.shape.graphics.beginFill(0xFFFFFF, 1);
|
||||
this.shape.graphics.drawRect(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight);
|
||||
this.shape.graphics.endFill();
|
||||
scene.addChild(this.shape);
|
||||
}
|
||||
|
||||
public process(){
|
||||
this.drawFullscreenQuad();
|
||||
}
|
||||
|
||||
public onSceneBackBufferSizeChanged(newWidth: number, newHeight: number){}
|
||||
|
||||
protected drawFullscreenQuad(){
|
||||
this.scene.filters = [this.effect];
|
||||
// this.shape.filters = [this.effect];
|
||||
}
|
||||
|
||||
public unload(){
|
||||
if (this.effect){
|
||||
this.effect = null;
|
||||
}
|
||||
|
||||
this.scene.removeChild(this.shape);
|
||||
this.scene = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
class GaussianBlurPostProcessor extends PostProcessor {
|
||||
public onAddedToScene(scene: Scene){
|
||||
super.onAddedToScene(scene);
|
||||
this.effect = new GaussianBlurEffect();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
///<reference path="./Renderer.ts" />
|
||||
class DefaultRenderer extends Renderer {
|
||||
public render(scene: Scene) {
|
||||
let cam = this.camera ? this.camera : scene.camera;
|
||||
this.beginRender(cam);
|
||||
|
||||
for (let i = 0; i < scene.renderableComponents.count; i++){
|
||||
let renderable = scene.renderableComponents.buffer[i];
|
||||
if (renderable.enabled && renderable.isVisibleFromCamera(cam))
|
||||
this.renderAfterStateCheck(renderable, cam);
|
||||
}
|
||||
}
|
||||
}
|
||||