Skip to content

Commit

Permalink
Spawnfix for clock and mine
Browse files Browse the repository at this point in the history
Reworked timers for clock and mine
- Added own despanwtimers for clock and mine
- Added restart functionality in timer.js
- Timer must be started manually

fixes #3
  • Loading branch information
MerGatto committed Feb 14, 2016
1 parent 05564b3 commit 84371ad
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 50 deletions.
8 changes: 4 additions & 4 deletions scripts/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ function DrawDebugInfo() {
text.innerHTML += "yPos: " + Math.round(player.yPos * 100) / 100 + "<br>";
text.innerHTML += "laser speed x: " + Math.round(laser.speed.x * 100) / 100 + " <br>";
text.innerHTML += "laser speed y: " + Math.round(laser.speed.y * 100) / 100 + " <br>";
if (spawnMineTimer == null) {
if (mineSpawnTimer == null) {
text.innerHTML += "spawnMineTimer: " + "null" + " <br>";
} else {
text.innerHTML += "spawnMineTimer: " + spawnMineTimer.timerId + " <br>";
text.innerHTML += "spawnMineTimer: " + mineSpawnTimer.timerId + " <br>";
}
if (spawnClockTimer == null) {
if (clockSpawnTimer == null) {
text.innerHTML += "spawnClockTimer: " + "null" + " <br>";
} else {
text.innerHTML += "spawnClockTimer: " + spawnClockTimer.timerId + " <br>";
text.innerHTML += "spawnClockTimer: " + clockSpawnTimer.timerId + " <br>";
}
text.innerHTML += "control up: " + control.up + " <br>";
text.innerHTML += "control down: " + control.down + " <br>";
Expand Down
83 changes: 42 additions & 41 deletions scripts/laserdodge.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
" use strict";
"use strict";
var gameOver = true;
var pause = false;
var fullscreen = false;
Expand All @@ -22,8 +22,10 @@ var clock = null;
var mine = null;

var gameLoopInterval = null;
var spawnMineTimer = null;
var spawnClockTimer = null;
var mineSpawnTimer = null;
var mineDespawnTimer = null;
var clockSpawnTimer = null;
var clockDespawnTimer = null;

var player = {
obj: null,
Expand Down Expand Up @@ -278,6 +280,20 @@ function init() {
gameField.removeChild(star);
star = null;
}

if(mineSpawnTimer == null) {
mineSpawnTimer = new Timer(SpawnMine, mineSpawntime);
}
if(mineDespawnTimer == null) {
mineDespawnTimer = new Timer(DespawnMine, mineDespawntime);
}
if(clockSpawnTimer == null) {
clockSpawnTimer = new Timer(SpawnClock, clockSpawntime);
}
if(clockDespawnTimer == null) {
clockDespawnTimer = new Timer(DespawnClock, clockDespawntime);
}

spawnStar();
}

Expand All @@ -290,8 +306,8 @@ function DoCountDown() {
txtCt.textContent = "";

gameLoopInterval = setInterval(gameloop, 1000 / GAMESPEED);
spawnMineTimer = new Timer(SpawnMine, mineSpawntime);
spawnClockTimer = new Timer(SpawnClock, clockSpawntime);
mineSpawnTimer.restart();
clockSpawnTimer.restart();
}
}

Expand All @@ -315,27 +331,27 @@ function PauseGame() {
if (!gameOver) {
pause = !pause;
if (pause) {
spawnClockTimer.pause();
spawnMineTimer.pause();
clockSpawnTimer.pause();
clockDespawnTimer.pause();
mineSpawnTimer.pause();
mineDespawnTimer.pause();
}
else {
spawnClockTimer.resume();
spawnMineTimer.resume();
clockSpawnTimer.resume();
mineSpawnTimer.resume();
clockDespawnTimer.pause();
mineDespawnTimer.pause();
}
}
}

function GameOver() {
control.pause = false;
clearInterval(gameLoopInterval);
if (spawnClockTimer != null) {
spawnClockTimer.pause();
spawnClockTimer = null;
}
if (spawnMineTimer != null) {
spawnMineTimer.pause();
spawnMineTimer = null;
}
clockSpawnTimer.pause();
clockDespawnTimer.pause();
mineSpawnTimer.pause();
mineDespawnTimer.pause();
gameLoopInterval = null;
document.getElementById("gameOverScreen").setAttribute("visibility", "visible");
document.getElementById("gameOverScore").textContent = "Sie haben "+score + " Punkte erzielt"
Expand Down Expand Up @@ -369,23 +385,17 @@ function SpawnMine() {

gameField.appendChild(mine);

if (spawnMineTimer != null) {
spawnMineTimer.pause;
spawnMineTimer = null;
}
spawnMineTimer = new Timer(DespawnMine, mineDespawntime);
mineSpawnTimer.pause();
mineDespawnTimer.restart()
}

function DespawnMine() {
if (mine != null) {
gameField.removeChild(mine);
mine = null;
}
if (spawnMineTimer != null) {
spawnMineTimer.pause;
spawnMineTimer = null;
}
spawnMineTimer = new Timer(SpawnMine, mineSpawntime);
mineDespawnTimer.pause();
mineSpawnTimer.restart();
}

function SpawnClock() {
Expand All @@ -403,28 +413,19 @@ function SpawnClock() {
clock.setAttributeNS("http://www.w3.org/1999/xlink", "href", "#clock");

gameField.appendChild(clock);
/*
clock.animate([
{ transform: 'rotate(360deg)' },
], clockDespawntime);*/
//ClockAnimation(clock);

if (spawnClockTimer != null) {
spawnClockTimer.pause();
spawnClockTimer = null;
}
spawnClockTimer = new Timer(DespawnClock, clockDespawntime);
clockSpawnTimer.pause();
clockDespawnTimer.restart();
}

function DespawnClock() {
if (clock != null) {
gameField.removeChild(clock);
clock = null;
}
if (spawnClockTimer != null) {
spawnClockTimer.pause();
spawnClockTimer = null;
}
spawnClockTimer = new Timer(SpawnClock, clockSpawntime);
clockDespawnTimer.pause();
clockSpawnTimer.restart();
}

function ClockAnimation(e) {
Expand Down
14 changes: 9 additions & 5 deletions scripts/timer.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
function Timer(callback, delay) {
this.start;
this.remaining = delay;
var start, remaining = delay;
this.time = delay;

this.pause = function () {
clearTimeout(this.timerId);
remaining -= performance.now() - start;
this.remaining -= performance.now() - this.start;
};

this.resume = function () {
start = performance.now();
this.start = performance.now();
clearTimeout(this.timerId);
this.timerId = window.setTimeout(callback, remaining);
this.timerId = window.setTimeout(callback, this.remaining);
};

this.resume();
this.restart = function() {
this.start = performance.now();
clearTimeout(this.timerId);
this.timerId = window.setTimeout(callback, this.time);
}
}

0 comments on commit 84371ad

Please sign in to comment.