You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
51 lines
1.1 KiB
let AC_GAME_OBJECTS = []; |
|
|
|
class AcGameObject { |
|
constructor() { |
|
AC_GAME_OBJECTS.push(this); |
|
this.time_delta = 0; // 距离上一帧的事件间隔 |
|
this.has_call_start = false; // 是否调用过start函数 |
|
} |
|
|
|
start() { // 最开始执行一次 |
|
|
|
} |
|
|
|
update() { // 每一帧执行一次(除了第一帧) |
|
|
|
} |
|
|
|
destory() { |
|
for (let i in AC_GAME_OBJECTS) { |
|
if (AC_GAME_OBJECTS[i] === this) { |
|
AC_GAME_OBJECTS.splice(i,1); |
|
break; |
|
} |
|
} |
|
|
|
} |
|
} |
|
|
|
let last_timestamp; // 上一帧执行完的时刻 |
|
|
|
// timestamp: 当前时刻 |
|
let AC_GAME_OBJECT_FRAME = (timestamp) => { |
|
for (let obj of AC_GAME_OBJECTS) { // 所有对象执行一次start函数 |
|
if (obj.has_call_start === false) { |
|
obj.start(); |
|
obj.has_call_start = true; |
|
} |
|
else { |
|
obj.time_delta = timestamp - last_timestamp; |
|
obj.update(); |
|
} |
|
} |
|
last_timestamp = timestamp; |
|
requestAnimationFrame(AC_GAME_OBJECT_FRAME); // 递归执行该函数 |
|
} |
|
|
|
requestAnimationFrame(AC_GAME_OBJECT_FRAME); |
|
|
|
export { |
|
AcGameObject, |
|
} |