Commit f95898a6 authored by Master Q's avatar Master Q

游戏 体验 加速度

parent 90bc30e8
......@@ -11,6 +11,7 @@ export class GPool {
*/
public static takeOut<T>(name: string): T {
if (this.pool[name] && this.pool[name].length) {
// console.log('从对象池中qu');
return this.pool[name].shift();
}
return null;
......
......@@ -62,17 +62,23 @@ export class ConveyorBelt extends FYGE.Container {
spl: number // 下一个 出现的 距离 后面就去取 随机值
tanValue: number
totalDisY: number // 全部距离
ty: number = 0 // y 经历的距离
ty: number = 0 // y 经历的距离 用来判断是否出下一个
isMove: boolean = false // 是否已经开始
clock: Clock
conveyorItems: Array<ConveyorBeltItemConstruct> = []
scaleEndProportion: number = 0.3
_config: ConveyorItemConfig
totalTy: number = 0 // 一共走了多少距离 计算分数
_totalTy: number = 0 // 一共走了多少距离 计算分数
updateScore: Function
acc: number = 0 // 加速度
get speed() {
return GameDiffConfig.getCurrGameDiff(this.getScore()).speed
return this._speed
}
set speed(n) {
this.initAcc()
this._speed = n
}
constructor(props: ConveyorBeltInfer, splt?: number, config: ConveyorItemConfig = defaultConveyorConfig) {
......@@ -87,9 +93,18 @@ export class ConveyorBelt extends FYGE.Container {
this.updateScore = props.updateScore
this.tanValue = Math.abs(props.start.x - props.end.x) ? (props.start.y - props.end.y) / (props.start.x - props.end.x) : 0
this.totalDisY = Math.abs(props.start.y - props.end.y)
this.initEvents()
}
/**
* 计算加速度
*/
initAcc() {
let t = this.totalDisY / this.speed
this.acc = 2 * this.totalDisY / Math.pow(t, 2)
}
reStart() {
this.conveyorItems.forEach(i => {
this.popConveyor(i)
......@@ -132,8 +147,7 @@ export class ConveyorBelt extends FYGE.Container {
// item = GPool.takeOut<GameEleOfWater>(e.resName) || new GameEleOfWater(e.resName)
// }
item.setPostion(this.startPos.x, this.startPos.y)
item.scale.set(0)
item.visible = true
item.reset()
this.conveyorItems.push(item)
return item
}
......@@ -150,7 +164,7 @@ export class ConveyorBelt extends FYGE.Container {
return this._config.conveyorItemsList[0]
}
const p = Math.random()
const currDiff = GameDiffConfig.getCurrGameDiff(this.getScore()).proportion
const currDiff = this.currConfig.proportion
// this._config.conveyorItemsList
const r = currDiff.findIndex((n: number) => {
return p < n
......@@ -174,40 +188,66 @@ export class ConveyorBelt extends FYGE.Container {
this.addChild(con)
}
/**
* 获取分数
*/
getScore() {
return Math.floor(this.totalTy / 10)
get totalTy() {
return this._totalTy
}
_score: number = 0
currConfig: any
set totalTy(n) {
this._score = Math.floor(n / 10)
this.currConfig = GameDiffConfig.getCurrGameDiff(this._score, (curr) => {
// this.speed = curr.speed
})
this.speed = this.currConfig.speed
this._totalTy = n
}
onChangeTime(t: number) {
this.conveyorItems.forEach(item => {
let _y = item.speed * t // vt + 1/2 *a t^2
let _x = _y / this.tanValue
// item.setPostion()
item.position.set(item.x + _x, item.y + _y)
const yp = _y / (this.totalDisY * this.scaleEndProportion)
item.scaleX < 1 && (item.scaleX += yp);
item.scaleX < 1 && (item.scaleY += yp);
if (item.y >= this.endPos.y) {
this.popConveyor(item)
}
item.speed += this.acc * t
})
}
onChangePosByY(changeY: number) {
this.ty += changeY
this.totalTy += changeY
this.updateScore && this.updateScore(this.getScore())
this.updateScore && this.updateScore(this._score)
if (this.ty >= this.spl) {
// console.log('渲染一个:', this.ty)
this.ty = 0
this.onRenderConveyorItem()
this.obstacleJudge && this.obstacleJudge(null, this)
}
this.conveyorItems.forEach(target => {
if (!this.tanValue) {
} else {
target.x += changeY / this.tanValue
}
target.y += changeY
const yp = changeY / (this.totalDisY * this.scaleEndProportion)
// target.alpha += yp
target.scaleX < 1 && (target.scaleX += yp);
target.scaleX < 1 && (target.scaleY += yp);
if (target.y >= this.endPos.y) {
this.popConveyor(target)
}
this.obstacleJudge && this.obstacleJudge(target, this)
})
// if (target.y >= RabbitPos.y + 200) {
// this.ConveyorBeltConfigData.onBehindFromRabbit(target)
// this.conveyorItems.forEach(target => {
// if (!this.tanValue) {
// } else {
// target.x += changeY / this.tanValue
// }
// target.y += changeY
// const yp = changeY / (this.totalDisY * this.scaleEndProportion)
// // target.alpha += yp
// target.scaleX < 1 && (target.scaleX += yp);
// target.scaleX < 1 && (target.scaleY += yp);
// if (target.y >= this.endPos.y) {
// this.popConveyor(target)
// this.obstacleJudge && this.obstacleJudge(target, this)
// }
// })
}
// 因为碰撞或者移动出去的 销毁
......@@ -228,6 +268,7 @@ export class ConveyorBelt extends FYGE.Container {
const currSpeed = this.speed
this.onChangePosByY(diff * currSpeed)
this.onChangeTime(diff)
}
}
\ No newline at end of file
......@@ -18,6 +18,8 @@ type GameDiffConfigType = {
export const GameDiffConfig: GameDiffConfigType = (function() {
let proportionList:ProportionList = []
let _i = null // 暂作比较
let _lcurr = null
return {
initGameDiff(pl: ProportionList) {
proportionList = pl || [
......@@ -29,10 +31,10 @@ export const GameDiffConfig: GameDiffConfigType = (function() {
{
mileage: 120,
proportion: [0.5,0.7,1],
speed: 330,
speed: 530,
},
{
mileage: 2200,
mileage:2200,
proportion: [0.1,0.7,1],
speed: 500,
}
......@@ -42,8 +44,16 @@ export const GameDiffConfig: GameDiffConfigType = (function() {
* 获取当前 概率
* @param m
*/
getCurrGameDiff(m: number):CurrProportion {
getCurrGameDiff(m: number, cb?: Function):CurrProportion {
const currIndex = proportionList.findIndex(i => m < i.mileage)
if (currIndex != _i) {
const curr = _lcurr = currIndex == -1 ? proportionList.slice(-1)[0] : proportionList[currIndex]
_i = currIndex
cb(curr)
return curr
} else {
return _lcurr
}
// console.log(currIndex, proportionList, proportionList.slice(currIndex, 1))
const curr = currIndex == -1 ? proportionList.slice(-1)[0] : proportionList[currIndex]
return curr
......@@ -59,6 +69,7 @@ export type GameEleType = 'obstacle' | 'unObstacle'
interface GameEleInfer {
_type: GameEleType,
_res: string,
speed: number, // 当前速度
getMaterialCollisionObj: () => CollisionData, // 获取碰撞
setPostion: (x: number, y: number) => void // 配置坐标
onCollision: () => void
......@@ -69,6 +80,7 @@ interface GameEleOfGamerPropsInfer {
}
export class GameEleOfGamer extends FYGE.Container implements GameEleInfer {
speed: number
_type: GameEleType = 'obstacle'
_res: string;
_isSpeedUp: boolean = false
......@@ -154,7 +166,7 @@ export class GameEleOfGamer extends FYGE.Container implements GameEleInfer {
reset() {
this.pxlIndex = 1
this.fy = 1130
this.fy = 1030
}
/**
......@@ -172,7 +184,7 @@ export class GameEleOfGamer extends FYGE.Container implements GameEleInfer {
this.reset()
}
setPostion(x: number, y: number=1130) {
setPostion(x: number, y: number=1030) {
this.fx = x
this.fy = y
// this.position.set(x - this.offsetX, y - this.offsetY)
......@@ -207,6 +219,7 @@ export class GameEleOfGamer extends FYGE.Container implements GameEleInfer {
}
export class GameEleOfPowerBall extends FYGE.Container implements GameEleInfer {
speed: number = 0
_type: GameEleType = 'unObstacle'
_res: string
offsetX: number = 130
......@@ -227,6 +240,7 @@ export class GameEleOfPowerBall extends FYGE.Container implements GameEleInfer {
this.addChild(new FYGE.MovieClip(RES.getRes(mv)))
this._res = mv
this.anchor.set(this.offsetX, this.offsetY)
this.reset()
const t = this.getMaterialCollisionObj()
Tools.PAGE.debug && UI.Rect(this, t.w, t.h, 0xcccccc, 20, 0, this.rHeight * (1-this.collisionProportionOfY), 0.7)
}
......@@ -235,6 +249,12 @@ export class GameEleOfPowerBall extends FYGE.Container implements GameEleInfer {
this.position.set(x - this.offsetX, y - this.offsetY)
}
reset() {
this.speed = 0
this.scale.set(0)
this.visible = true
}
onCollision() {
}
......@@ -250,6 +270,7 @@ export class GameEleOfPowerBall extends FYGE.Container implements GameEleInfer {
}
export class GameEleOfWater extends FYGE.Sprite implements GameEleInfer {
speed: number = 0
_type: GameEleType = 'obstacle'
_res: string
collisionProportionOfY: number = 0.5
......@@ -259,6 +280,7 @@ export class GameEleOfWater extends FYGE.Sprite implements GameEleInfer {
this._res = res
this.anchorTexture.set(0.5, 0.5)
const t = this.getMaterialCollisionObj()
this.reset()
Tools.PAGE.debug && UI.Rect(this, t.w, t.h, 0xcccccc, 20, 0 - this.width / 2, this.height * (1-this.collisionProportionOfY) - this.height / 2, 0.7)
}
......@@ -275,17 +297,20 @@ export class GameEleOfWater extends FYGE.Sprite implements GameEleInfer {
}
}
onCollision() {
reset() {
this.speed = 0
this.scale.set(0)
this.visible = true
}
reset() {
onCollision() {
}
}
export class GameEleOfBucket extends FYGE.Sprite implements GameEleInfer {
speed: number;
_type: GameEleType = 'obstacle'
_res: string
collisionProportionOfY: number = 0.4
......@@ -295,10 +320,17 @@ export class GameEleOfBucket extends FYGE.Sprite implements GameEleInfer {
this._res = res
this.anchorTexture.set(0.5, 0.5)
const t = this.getMaterialCollisionObj()
this.reset()
Tools.PAGE.debug && UI.Rect(this, t.w, t.h, 0xcccccc, 20, 0- this.width / 2, this.height * (1-this.collisionProportionOfY) - this.height / 2, 0.7)
}
reset() {
this.speed = 0
this.scale.set(0)
this.visible = true
}
setPostion(x: number, y: number=0) {
this.position.set(x, y)
}
......
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