Commit 5ca7642d authored by 邱旭's avatar 邱旭

08.FlppyBird-模拟重力

parent ffdce49d
...@@ -8,94 +8,36 @@ ...@@ -8,94 +8,36 @@
* 抽象了一个简单的GameObject * 抽象了一个简单的GameObject
*/ */
class GameObject { class GameObject {
id; // 绑定的dom元素的id id; // 绑定的dom元素的id
dom; // 绑定的dom元素 dom; // 绑定的dom元素
/** // 位置
* transform 表示显示对象的变换 top = 0;
* @type {{rotate: number, scale: {x: number, y: number}, position: {top: number, left: number}}} left = 0;
*/
transform = {
position: { top: 0, left: 0 }, // 位置
scale: { x: 1, y: 1 }, // 缩放
rotate: 0, // 旋转
}
constructor(id) { // 缩放
this.id = id; scaleX = 1;
this.dom = document.getElementById(id); // 在构造函数中绑定dom元素 scaleY = 1;
this.dom.style.position = "absolute";
}
// 旋转
rotate = 0;
/** /**
* 获得宽高 * 获得宽高
* @returns {{width: number, height: number}} * @returns {{width: number, height: number}}
*/ */
getSize() { get size() {
return { return {
width: this.dom.clientWidth, width: this.dom.clientWidth,
height: this.dom.clientHeight, height: this.dom.clientHeight,
} }
} }
constructor(id) {
/** this.id = id;
* 设置Position this.dom = document.getElementById(id); // 在构造函数中绑定dom元素
* @param top this.dom.style.position = "absolute";
* @param left
*/
setPosition(top = this.transform.position.top, left = this.transform.position.left) {
this.transform.position.top = top;
this.transform.position.left = left;
}
/**
* 设置Scale
* @param x
* @param y
*/
setScale(x = this.transform.scale.x, y = this.transform.scale.y) {
this.transform.scale.x = x;
this.transform.scale.y = y;
}
/**
* 设置Rotate
* @param rotate
*/
setRotate(rotate = this.transform.rotate) {
this.transform.rotate = rotate;
}
/**
* 获得Position
* @returns {{top: number, left: number}}
*/
getPosition() {
return {
top: this.transform.position.top,
left: this.transform.position.left,
}
}
/**
* 获得Scale
* @returns {{x: number, y: number}}
*/
getScale() {
return {
x: this.transform.scale.x,
y: this.transform.scale.y,
}
}
/**
* 获得Rotate
* @returns {number}
*/
getRotate() {
return this.transform.rotate;
} }
/** /**
...@@ -109,9 +51,7 @@ class GameObject { ...@@ -109,9 +51,7 @@ class GameObject {
* 抽离渲染部分 * 抽离渲染部分
*/ */
render() { render() {
const { top, left } = this.getPosition(); const { top, left, scaleX, scaleY, rotate } = this;
const { x: scaleX, y: scaleY } = this.getScale();
const rotate = this.getRotate();
this.dom.style.top = top + "px"; this.dom.style.top = top + "px";
this.dom.style.left = left + "px"; this.dom.style.left = left + "px";
...@@ -124,4 +64,5 @@ class GameObject { ...@@ -124,4 +64,5 @@ class GameObject {
destroy() { destroy() {
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment