Commit 6940f7b7 authored by 邱旭's avatar 邱旭

06.面向对象

parent 38f2c6da
...@@ -29,13 +29,16 @@ ...@@ -29,13 +29,16 @@
constructor(id, speed) { constructor(id, speed) {
super(id); super(id);
this.speed = speed; // 保存speed this.speed = speed; // 保存speed
this.setPosition(150, 150); // 给个默认位置
// 给个默认位置
this.top = 150;
this.left = 150;
} }
update() { update() {
super.update(); super.update();
const { top, left } = this.getPosition(); const { top, left } = this;
const speed = this.speed; const speed = this.speed;
// 计算新的top // 计算新的top
...@@ -45,9 +48,9 @@ ...@@ -45,9 +48,9 @@
// 如果速度过快,本次移动直接过头了(即差值<速度),就直接移动到指定top // 如果速度过快,本次移动直接过头了(即差值<速度),就直接移动到指定top
if (Math.abs(dis) < speed) { if (Math.abs(dis) < speed) {
this.transform.position.top = clickPos.top; this.top = clickPos.top;
} else { } else {
this.transform.position.top = top + dir * speed; // 计算新的top,新的位置 = 之前的位置 + 方向 * 速度 this.top = top + dir * speed; // 计算新的top,新的位置 = 之前的位置 + 方向 * 速度
} }
} }
...@@ -57,9 +60,9 @@ ...@@ -57,9 +60,9 @@
const dir = dis > 0 ? 1 : -1; const dir = dis > 0 ? 1 : -1;
if (Math.abs(dis) < speed) { if (Math.abs(dis) < speed) {
this.transform.position.left = clickPos.left; this.left = clickPos.left;
} else { } else {
this.transform.position.left = left + dir * speed; this.left = left + dir * speed;
} }
} }
} }
......
...@@ -18,81 +18,36 @@ ...@@ -18,81 +18,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;
this.dom = document.getElementById(id); // 在构造函数中绑定dom元素
this.dom.style.position = "absolute";
}
/** // 缩放
* 设置Position scaleX = 1;
* @param top scaleY = 1;
* @param left
*/
setPosition(top = this.transform.position.top, left = this.transform.position.left) {
this.transform.position.top = top;
this.transform.position.left = left;
}
/** // 旋转
* 设置Scale rotate = 0;
* @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 * @returns {{width: number, height: number}}
*/ */
setRotate(rotate = this.transform.rotate) { get size() {
this.transform.rotate = rotate;
}
/**
* 获得Position
* @returns {{top: number, left: number}}
*/
getPosition() {
return { return {
top: this.transform.position.top, width: this.dom.clientWidth,
left: this.transform.position.left, height: this.dom.clientHeight,
} }
} }
/** constructor(id) {
* 获得Scale this.id = id;
* @returns {{x: number, y: number}} this.dom = document.getElementById(id); // 在构造函数中绑定dom元素
*/ this.dom.style.position = "absolute";
getScale() {
return {
x: this.transform.scale.x,
y: this.transform.scale.y,
}
}
/**
* 获得Rotate
* @returns {number}
*/
getRotate() {
return this.transform.rotate;
} }
/** /**
...@@ -106,13 +61,11 @@ class GameObject { ...@@ -106,13 +61,11 @@ 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";
this.dom.style.transform = `translate(-50%, -50%) scale(${scaleX}, ${scaleY}) rotate(${rotate}deg)`; this.dom.style.transform = `scale(${scaleX}, ${scaleY}) rotate(${rotate}deg)`;
} }
/** /**
...@@ -121,26 +74,30 @@ class GameObject { ...@@ -121,26 +74,30 @@ class GameObject {
destroy() { destroy() {
} }
} }
``` ```
- 2.用面向对象的方式创建鸟的class - 2.用面向对象的方式创建鸟的class
```javascript ```javascript
class Bird extends GameObject { class Bird extends GameObject {
speed; // bird的速度 每次移动多少距离 speed; // bird的速度 每次移动多少距离
constructor(id, speed = 15) { constructor(id, speed) {
super(id); super(id);
this.speed = speed; // 保存speed this.speed = speed; // 保存speed
this.setPosition(150, 150); // 给个默认位置
// 给个默认位置
this.top = 150;
this.left = 150;
} }
update() { update() {
super.update(); super.update();
const { top, left } = this.getPosition(); const { top, left } = this;
const speed = this.speed; const speed = this.speed;
// 计算新的top // 计算新的top
...@@ -150,9 +107,9 @@ class Bird extends GameObject { ...@@ -150,9 +107,9 @@ class Bird extends GameObject {
// 如果速度过快,本次移动直接过头了(即差值<速度),就直接移动到指定top // 如果速度过快,本次移动直接过头了(即差值<速度),就直接移动到指定top
if (Math.abs(dis) < speed) { if (Math.abs(dis) < speed) {
this.transform.position.top = clickPos.top; this.top = clickPos.top;
} else { } else {
this.transform.position.top = top + dir * speed; // 计算新的top,新的位置 = 之前的位置 + 方向 * 速度 this.top = top + dir * speed; // 计算新的top,新的位置 = 之前的位置 + 方向 * 速度
} }
} }
...@@ -162,13 +119,14 @@ class Bird extends GameObject { ...@@ -162,13 +119,14 @@ class Bird extends GameObject {
const dir = dis > 0 ? 1 : -1; const dir = dis > 0 ? 1 : -1;
if (Math.abs(dis) < speed) { if (Math.abs(dis) < speed) {
this.transform.position.left = clickPos.left; this.left = clickPos.left;
} else { } else {
this.transform.position.left = left + dir * speed; this.left = left + dir * speed;
} }
} }
} }
} }
``` ```
- 3.创建鸟的实例 - 3.创建鸟的实例
......
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