Skip to content

Commit

Permalink
Merge pull request #12 from keguigong/dev
Browse files Browse the repository at this point in the history
Add HP bar and mountain
  • Loading branch information
keguigong authored Nov 22, 2023
2 parents f89ab88 + dad0cbc commit 9b48c7a
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 27 deletions.
4 changes: 2 additions & 2 deletions game/DistanceMeter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export default class DistanceMeter {
ctx!: CanvasRenderingContext2D
spritePos!: Position

x = 0
y = 0
x = 2
y = 2

distance = 0
maxScore = 0
Expand Down
130 changes: 130 additions & 0 deletions game/HPBar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import Runner from "./Runner"
import { IS_HIDPI } from "./varibles"

export default class HPBar {
canvas!: HTMLCanvasElement
ctx!: CanvasRenderingContext2D
spritePos!: Position
canvasWidth = 0

x = 0
y = 0

hp = 3
hpChanged = false
flashTimer = 0
flashIterations = 0

opacity = 0

constructor(canvas: HTMLCanvasElement, spritePos: Position, canvasWidth: number) {
this.canvas = canvas
this.ctx = canvas.getContext("2d")!
this.spritePos = spritePos
this.canvasWidth = canvasWidth
}

update(deltaTime: number, hp: number) {
let paint = true
if (this.opacity < 1) {
this.opacity = Math.min(this.opacity + HPBar.config.FADE_SPEED, 1)
}

if (!this.hpChanged) {
this.hpChanged = hp !== this.hp

this.flashIterations = 0
this.flashTimer = 0
} else {
if (this.flashIterations <= HPBar.config.FLASH_ITERATIONS) {
this.flashTimer += deltaTime

if (this.flashTimer < HPBar.config.FLASH_DURATION) {
paint = false
} else if (this.flashTimer > HPBar.config.FLASH_DURATION * 2) {
this.flashTimer = 0
this.flashIterations++
}
} else {
this.hpChanged = false
this.flashIterations = 0
this.flashTimer = 0
}
}

this.hp = hp

// Draw the digits if not flashing.
if (paint) {
for (let i = 0; i <= HPBar.config.MAX_HP - 1; i++) {
if (i <= this.hp - 1) {
this.draw(i, 0)
} else {
this.draw(i, 1)
}
}
}
}

draw(targetPos: number, sourcePos: number) {
let sourceWidth = HPBar.dimensions.WIDTH
let sourceHeight = HPBar.dimensions.HEIGHT
let sourceX = HPBar.dimensions.WIDTH * sourcePos
let sourceY = 0

let targetX = targetPos * HPBar.dimensions.DEST_WIDTH
let targetY = this.y
let targetWidth = HPBar.dimensions.WIDTH
let targetHeight = HPBar.dimensions.HEIGHT

if (IS_HIDPI) {
sourceWidth *= 2
sourceHeight *= 2
sourceX *= 2
}

sourceX += this.spritePos.x
sourceY += this.spritePos.y

this.ctx.save()
this.ctx.globalAlpha = this.opacity

this.ctx.drawImage(
Runner.imageBdaySprite,
sourceX,
sourceY,
sourceWidth,
sourceHeight,
targetX,
targetY,
targetWidth,
targetHeight
)

this.ctx.restore()
this.ctx.globalAlpha = 1
}

reset() {
this.hp = HPBar.config.MAX_HP
this.hpChanged = false
this.flashTimer = 0
this.flashIterations = 0

this.update(0, this.hp)
}

static config = {
FLASH_DURATION: 1000 / 4, // 一闪的时间(一次闪动分别两闪:从有到无,从无到有)
FLASH_ITERATIONS: 3, // 闪动的次数
MAX_HP: 3,
HP_UNIT: 1,
FADE_SPEED: 0.035 // 淡入淡出的速度
}

static dimensions = {
WIDTH: 28,
HEIGHT: 26,
DEST_WIDTH: 30
}
}
5 changes: 5 additions & 0 deletions game/Horizon.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Cloud from "./Cloud"
import HorizonLine from "./HorizonLine"
import Mountain from "./Mountain"
import NightMode from "./NightMode"
import Obstacle from "./Obstacle"
import Runner from "./Runner"
Expand All @@ -22,6 +23,7 @@ export default class Horizon {
obstacleHistory: string[] = []

nightMode!: NightMode
mountain!: Mountain

constructor(canvas: HTMLCanvasElement, spritePos: SpritePosDef, dimensions: Dimensions, gapCoeffient: number) {
this.canvas = canvas
Expand All @@ -37,12 +39,14 @@ export default class Horizon {
this.addCloud()
this.horizonLine = new HorizonLine(this.canvas, this.spritePos.HORIZON)
this.nightMode = new NightMode(this.canvas, this.spritePos.MOON, this.dimensions.WIDTH)
this.mountain = new Mountain(this.canvas, Runner.bdaySpriteDefinition.MOUNTAIN, this.dimensions.WIDTH)
}

update(deltaTime: number, speed: number, hasObstacles?: boolean, showNightMode: boolean = false) {
this.horizonLine.update(deltaTime, speed)
this.nightMode.update(showNightMode)
this.updateCloud(deltaTime, speed)
this.mountain.update()
if (hasObstacles) {
this.updateObstacles(deltaTime, speed)
}
Expand Down Expand Up @@ -150,5 +154,6 @@ export default class Horizon {
this.obstacles = []
this.horizonLine.reset()
this.nightMode.reset()
this.mountain.reset()
}
}
78 changes: 78 additions & 0 deletions game/Mountain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import HorizonLine from "./HorizonLine"
import Runner from "./Runner"
import { IS_HIDPI } from "./varibles"

export default class Mountain {
canvas!: HTMLCanvasElement
ctx!: CanvasRenderingContext2D
spritePos!: Position
containerWidth = 0

xPos = Runner.defaultDimensions.WIDTH
yPos = 0

constructor(canvas: HTMLCanvasElement, spritePos: Position, containerWidth: number) {
this.canvas = canvas
this.ctx = canvas.getContext("2d")!
this.spritePos = spritePos
this.containerWidth = containerWidth

this.xPos = containerWidth
this.yPos = HorizonLine.dimensions.YPOS - Mountain.config.HEIGHT

this.draw()
}

updateXPos(currentPos: number, speed: number) {
if (currentPos < -Mountain.config.WIDTH) {
currentPos = this.containerWidth
} else {
currentPos -= speed
}
return currentPos
}

draw() {
let sourceWidth = Mountain.config.WIDTH
let sourceHeight = Mountain.config.HEIGHT
if (IS_HIDPI) {
sourceWidth *= 2
sourceHeight *= 2
}
this.ctx.save()

this.ctx.drawImage(
Runner.imageBdaySprite,
this.spritePos.x,
this.spritePos.y,
sourceWidth,
sourceHeight,
this.xPos,
this.yPos,
Mountain.config.WIDTH,
Mountain.config.HEIGHT
)

this.ctx.restore()
}

update() {
this.xPos = this.updateXPos(this.xPos, Mountain.config.MOUNTAIN_SPEED)
this.draw()
}

reset() {
this.xPos = this.containerWidth
this.draw()
}

protected isVisible() {
return this.xPos + Mountain.config.WIDTH > 0
}

static config = {
WIDTH: 413,
HEIGHT: 92,
MOUNTAIN_SPEED: 0.1
}
}
26 changes: 13 additions & 13 deletions game/Obstacle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,25 +214,25 @@ export default class Obstacle {
frameRate: 1000 / 6,
speedOffset: 0.8
},
{
type: "BIRTHDAY_CAKE",
width: 33,
height: 40,
yPos: 90,
multipleSpeed: 999,
minGap: 100,
minSpeed: 0,
collisionBoxes: [new CollisionBox(13, 1, 6, 12), new CollisionBox(6, 13, 20, 4), new CollisionBox(3, 18, 27, 19)]
},
// {
// type: "BIRTHDAY_CAKE",
// width: 33,
// height: 40,
// yPos: 90,
// multipleSpeed: 999,
// minGap: 100,
// minSpeed: 0,
// collisionBoxes: [new CollisionBox(13, 1, 6, 12), new CollisionBox(6, 13, 20, 4), new CollisionBox(3, 18, 27, 19)]
// }
{
type: "HP",
width: 32,
height: 30,
width: 28,
height: 26,
yPos: [100, 75, 50],
multipleSpeed: 999,
minGap: 100,
minSpeed: 0,
collisionBoxes: [new CollisionBox(0, 0, 32, 30)]
collisionBoxes: [new CollisionBox(0, 0, 26, 24)]
}
]

Expand Down
Loading

0 comments on commit 9b48c7a

Please sign in to comment.