Add score UI using sprite digits
This commit is contained in:
parent
3edc195aea
commit
7f5363d3dc
85
index.html
85
index.html
@ -127,7 +127,7 @@
|
|||||||
left:50px;
|
left:50px;
|
||||||
top: 50px;
|
top: 50px;
|
||||||
z-index: 10;
|
z-index: 10;
|
||||||
">Score</div>
|
"></div>
|
||||||
|
|
||||||
<div id="get_ready" style="
|
<div id="get_ready" style="
|
||||||
left: calc(50vw - 200px);
|
left: calc(50vw - 200px);
|
||||||
@ -141,6 +141,10 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
// POTENTIAL IMPROVEMENTS:
|
||||||
|
// - Use a fixed pool of level objects instead of removing and creating them all the time
|
||||||
|
// - Use a better class hierarchy - there's some code duplication, especially between Spikes and Ground
|
||||||
|
|
||||||
// // Setup
|
// // Setup
|
||||||
|
|
||||||
// Scale viewport to fit device width
|
// Scale viewport to fit device width
|
||||||
@ -324,10 +328,66 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Digit {
|
||||||
|
constructor(x, y, image_x, image_y, width, height) {
|
||||||
|
this.position_x = x;
|
||||||
|
this.position_y = y;
|
||||||
|
|
||||||
|
this.width = width;
|
||||||
|
this.height = height
|
||||||
|
|
||||||
|
this.id = Math.floor(Math.random() * 1000000);
|
||||||
|
|
||||||
|
this.element1 = document.createElement("div");
|
||||||
|
this.element1.id = this.id;
|
||||||
|
|
||||||
|
document.getElementById("score").appendChild(this.element1);
|
||||||
|
|
||||||
|
$("#" + this.id).css({
|
||||||
|
"left": (this.position_x) + "px",
|
||||||
|
"top": (this.position_y) + "px",
|
||||||
|
"background-image": "url(sheet.png)",
|
||||||
|
"background-position": "-" + image_x + "px -" + image_y + "px",
|
||||||
|
"width": width + "px",
|
||||||
|
"height": height + "px",
|
||||||
|
"z-index": "10"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
remove() {
|
||||||
|
document.getElementById(this.id).remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function get_digit(val, x, y) {
|
||||||
|
if (val == 0) {
|
||||||
|
return new Digit(x, y, 432, 1743, 53, 78);
|
||||||
|
} else if (val == 1) {
|
||||||
|
return new Digit(x, y, 512, 1093, 37, 76);
|
||||||
|
} else if (val == 2) {
|
||||||
|
return new Digit(x, y, 477, 1350, 51, 77);
|
||||||
|
} else if (val == 3) {
|
||||||
|
return new Digit(x, y, 485, 1679, 51, 78);
|
||||||
|
} else if (val == 4) {
|
||||||
|
return new Digit(x, y, 432, 1537, 55, 76);
|
||||||
|
} else if (val == 5) {
|
||||||
|
return new Digit(x, y, 485, 1823, 50, 76);
|
||||||
|
} else if (val == 6) {
|
||||||
|
return new Digit(x, y, 432, 1885, 53, 77);
|
||||||
|
} else if (val == 7) {
|
||||||
|
return new Digit(x, y, 478, 1173, 51, 76);
|
||||||
|
} else if (val == 8) {
|
||||||
|
return new Digit(x, y, 461, 899, 51, 78);
|
||||||
|
} else if (val == 9) {
|
||||||
|
return new Digit(x, y, 458, 1962, 53, 77);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Objects
|
// Objects
|
||||||
let player = new Bird()
|
let player = new Bird()
|
||||||
var spikes = []
|
var spikes = []
|
||||||
var grounds = []
|
var grounds = []
|
||||||
|
var score_digits = []
|
||||||
|
|
||||||
// Utility functions
|
// Utility functions
|
||||||
function randomInt(min, max) {
|
function randomInt(min, max) {
|
||||||
@ -344,6 +404,22 @@
|
|||||||
next_ground_location += GROUND_DISTANCE;
|
next_ground_location += GROUND_DISTANCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function update_score() {
|
||||||
|
score_digits.forEach(function (digit) {
|
||||||
|
digit.remove();
|
||||||
|
})
|
||||||
|
score_digits.length = 0;
|
||||||
|
|
||||||
|
// Could be done with modulo and division but I like this stupid approach:
|
||||||
|
// Turn the score into a string and iterate over its characters
|
||||||
|
var working_x = 0;
|
||||||
|
String(score).split('').map(function (v, i) {
|
||||||
|
var digit = get_digit(v, working_x, 0);
|
||||||
|
score_digits.push(digit);
|
||||||
|
working_x += digit.width;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Setup the game world by removing old objects and spawning new ones
|
// Setup the game world by removing old objects and spawning new ones
|
||||||
function setupGame() {
|
function setupGame() {
|
||||||
next_spike_location = FIRST_SPIKE_POSITION;
|
next_spike_location = FIRST_SPIKE_POSITION;
|
||||||
@ -370,6 +446,9 @@
|
|||||||
for (let i = next_ground_location; i < INITIAL_SPIKE_DISTANCE; i += GROUND_DISTANCE) {
|
for (let i = next_ground_location; i < INITIAL_SPIKE_DISTANCE; i += GROUND_DISTANCE) {
|
||||||
spawn_ground();
|
spawn_ground();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
score = 0;
|
||||||
|
update_score();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fixed time loop
|
// Fixed time loop
|
||||||
@ -436,11 +515,9 @@
|
|||||||
// Increase the score if we just left spikes
|
// Increase the score if we just left spikes
|
||||||
if (under_spikes_in_previous_frame && !under_spikes_this_frame) {
|
if (under_spikes_in_previous_frame && !under_spikes_this_frame) {
|
||||||
score += 1;
|
score += 1;
|
||||||
|
update_score();
|
||||||
}
|
}
|
||||||
under_spikes_in_previous_frame = under_spikes_this_frame;
|
under_spikes_in_previous_frame = under_spikes_this_frame;
|
||||||
|
|
||||||
// TODO: Display as UI
|
|
||||||
console.log(score);
|
|
||||||
} else if (current_gamestate == GameState.STARTSCREEN) {
|
} else if (current_gamestate == GameState.STARTSCREEN) {
|
||||||
// Drop the player to the ground if they're not there yet (after hitting a spike)
|
// Drop the player to the ground if they're not there yet (after hitting a spike)
|
||||||
if (player.position_y < 400 && has_died) {
|
if (player.position_y < 400 && has_died) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user