-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from nathannlu/vindennt/feature/hunger
Vindennt/feature/hunger
- Loading branch information
Showing
4 changed files
with
234 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { GameObject } from "../core.js"; | ||
import { darkenHexColor } from "../../utils.js"; | ||
|
||
export class PointBar extends GameObject { | ||
constructor({ | ||
x, | ||
y, | ||
width, | ||
height, | ||
maxPoints, | ||
colour, | ||
label, | ||
fontSize = 14, | ||
fontFamily = "Courier New", | ||
fontWeight = "bold", | ||
associatedId = null, | ||
initialPoints = maxPoints, | ||
}) { | ||
super(x, y, width, height); | ||
this.maxPoints = maxPoints; | ||
this.currentPoints = initialPoints; | ||
this.colour = colour; | ||
this.label = label; | ||
this.fontSize = fontSize; | ||
this.fontFamily = fontFamily; | ||
this.fontWeight = fontWeight; | ||
// id of gameObject to be associated with i.e. the pet | ||
this.associatedId = associatedId; | ||
} | ||
|
||
addPoints(points) { | ||
this.currentPoints += points; | ||
if (this.currentPoints > this.maxPoints) { | ||
this.currentPoints = this.maxPoints; | ||
} | ||
} | ||
|
||
removePoints(points) { | ||
this.currentPoints -= points; | ||
if (this.currentPoints < 0) { | ||
this.currentPoints = 0; | ||
} | ||
} | ||
|
||
setPoints(points) { | ||
this.currentPoints = points; | ||
if (this.currentPoints > this.maxPoints) { | ||
this.currentPoints = this.maxPoints; | ||
} else if (this.currentPoints < 0) { | ||
this.currentPoints = 0; | ||
} | ||
} | ||
|
||
render(ctx) { | ||
// Fill background box | ||
const padding = 15; | ||
ctx.fillStyle = darkenHexColor(this.colour, 50); | ||
ctx.beginPath(); | ||
ctx.roundRect( | ||
this.x - padding, | ||
this.y, | ||
this.width + 2 * padding, | ||
this.height + 2 * padding, | ||
4 | ||
); | ||
ctx.fill(); | ||
|
||
// fill each line per point | ||
const lineSpacing = this.height / this.maxPoints; | ||
ctx.fillStyle = this.colour; | ||
for (let i = 0; i < this.currentPoints; i++) { | ||
const lineY = this.y + this.height - i * lineSpacing; | ||
ctx.fillRect(this.x, lineY, this.width, lineSpacing * 0.75); | ||
} | ||
|
||
// fill label | ||
ctx.font = `${this.fontWeight} ${this.fontSize}px ${this.fontFamily} `; | ||
ctx.fillStyle = "white"; | ||
ctx.textAlign = "center"; | ||
ctx.textBaseline = "middle"; | ||
ctx.fillText( | ||
this.label, | ||
this.x + this.width / 2, | ||
this.y + this.height + lineSpacing * 0.75 + 12 | ||
); | ||
} | ||
} |