Function Return: //setam Proprietatile Fiecarui Obiect
Function Return: //setam Proprietatile Fiecarui Obiect
{
return Math.random() * (max-min) + min;
}
var canvas;
var context;
var keyOn = [];
var Movable = function(data)
{
if (data === undefined)
return;
for (var i = 0; i < data.length; i++)
{
var setting = data[i];
//Setam proprietatile fiecarui obiect
this[setting[0]] = setting[1];
}
this.alive = true;
}
Movable.prototype = {
update: function()
{
if (this.alive)
{
this.move();
this.draw();
}
},
draw: function()
{
context.fillStyle = this.color;
context.fillRect(this.x, this.y, this.width, this.height);
}
};
var Countable = function()
{
this.x = 0;
this.y = 0;
this.speed = 2;
this.value = 0;
this.targetValue = 0;
}
Countable.prototype = {
update: function()
{
this.move();
this.draw();
},
change: function(amount)
{
this.targetValue += amount;
},
move: function()
{
//Schimbam pozitia obiectului doar daca este diferita de pozitia in
care se afla
if (this.value === this.targetValue)
return;
};
//------------------------------------------------------------------------------//Initializarea imaginilor
var tideSprites = new Array;
tideSprites[0] = new Image();
tideSprites[0].src = 'images/tideL.png';
tideSprites[1] = new Image();
tideSprites[1].src = 'images/tideR.png';
var foodSprites = new Array;
foodSprites[0] = new Image();
foodSprites[0].src = 'images/tango.png';
foodSprites[1] = new Image();
foodSprites[1].src = 'images/mana.png';
foodSprites[2] = new Image();
foodSprites[2].src = 'images/salve.png';
foodSprites[3] = new Image();
foodSprites[3].src = 'images/mine2.png';
foodSprites[4] = new Image();
foodSprites[4].src = 'images/inversemine.png';
//------------------------------------------------------------------------------var tideImage = tideSprites[1];
var foodImage = foodSprites[0];
var Tide = function(data)
{
Movable.call(this, data);
}
Tide.prototype = new Movable();
Tide.prototype.reset = function()
{
//Resetarea pozitiei
this.x = canvas.width / 2 - this.width / 2;
this.y = canvas.height - this.height;
}
Tide.prototype.draw = function()
{
//Afisare
context.drawImage(tideImage, this.x, this.y, this.width, this.height);
}
Tide.prototype.move = function()
{
//37 == Left
if (keyOn[37])
{
//Schimbam animatia si pozitia
tideImage = tideSprites[0];
this.x -= this.xSpeed;
}
//39 == Right
if (keyOn[39])
{
tideImage = tideSprites[1];
this.x += this.xSpeed;
}
//Daca pozitia pe axa Ox este < 0 inseamna ca obiectul iese din ecran
if (this.x < 0)
this.x = 0;
//Daca Daca pozitia pe axa Ox + lungimea obiectului este > lungimea
"ecranului":
//Inseamna ca obiectul iese din ecran
if (this.x + this.width > canvas.width)
this.x = canvas.width - this.width;
}
var Food = function(data)
{
Movable.call(this, data);
var foodImg;
this.initPosition();
this.initImage();
//Textul
context.fillStyle = '#000';
context.textBaseline = 'top';
context.fillText('HP', this.x - 25, this.y -3);
}
Score.prototype = new Countable();
Score.prototype.reset = function()
{
this.value = this.targetValue = 0;
}
Score.prototype.draw = function()
{
context.textBaseline = 'top';
context.fillStyle = '#000';
context.fillText(this.value, this.x, this.y);
context.fillText('PTS', this.x - 25, this .y);
}
Hunger = new function()
{
var tideData = [
['width', 150],
['height', 120],
['xSpeed', 4]
];
var foodData = [
['width', 25],
['height', 25],
['ySpeed', 1],
['strength', 30],
['isBomb', 0]
];
var
var
var
var
var
foodPerLevel = 4;
foodSpawnSec = 0;
foodSpawned = 0;
foodOnScreen = 0;
food = [];
var level;
var levelUp;
var tide;
var health;
var score;
var info;
var infoScreenChange = true;
var startTime;
var frameTime;
this.run = function()
{
//Initializam fereastra pentru afisaj
canvas = document.getElementById('canvas');
info = document.getElementById('info');
context = canvas.getContext('2d');
var bgImg = new Image();
bgImg.src = 'images/bg.jpg';
context.drawImage(bgImg, 0, 0);
//Retinem orice tasta apasata
document.addEventListener('keydown', function(event)
{
//Marcam in vectorul keyOn pozitia tastei apasate cu true
keyOn[event.keyCode] = true;
}, false);
//Stergem din vectorul keyOn tastele ce au fost eliberate
document.addEventListener('keyup', function(event)
{
keyOn[event.keyCode] = false;
}, false);
//Instantierea obiectelor
tide = new Tide(tideData);
health = new Health();
score = new Score();
//Setam cate cadre pe secunda se vor afisa
interval = setInterval(titleScreen, 40/1000);
}
function resetGame()
{
tide.reset();
health.reset();
score.reset();
foodSpawnSec = 2.5;
foodSpawned = 0;
foodOnScreen = 0;
foods = [];
level = 1;
startTime = new Date().getTime();
}
function resetLevel()
{
tide.reset();
health.reset();
foodSpawned = 0;
foodOnScreen = 0;
foods = [];
function titleScreen()
{
if (infoScreenChange)
{
info.innerHTML = '<p class="text">Hunger In The Depths</p> <p
class="text">Press <b>spacebar</b> to start</p>';
}
infoScreenChange = false;
if (keyOn[32])
{
infoScreenChange = true;
info.style.display = 'none';
clearInterval(interval);
resetGame();
}
}
function gameOverScreen()
{
frameTime = new Date().getTime();
if (infoScreenChange)
{
info.innerHTML = '<p>Game over!</p>';
info.style.display = 'block';
}
infoScreenChange = false;
clearInterval(interval);
interval = setInterval(titleScreen, 30/1000);
}
function gameLoop()
{
frameTime = new Date().getTime();
if (health.value < 1)
{
tide.alive = false;
clearInterval(interval);
interval = setInterval(gameOverScreen, 30/1000);
}
return;
if (levelUp)
{
if (infoScreenChange)
{
startTime = frameTime;
return;
function updateObjects()
{
for (var i = 0; i < foods.length; i++)
{
//Copiem obiectul intr-o var locala
var food = foods[i];
food.update();
checkCollision(food);
}
foodOnScreen--;
score.change(-food.strength);
health.change(-food.strength);
}
else
{
&& food.strength
food.strength = 0;
}
if (food.alive === true && food.y > canvas.height)
{
food.alive = false;
foodOnScreen--;
}
}
function addObjects()
{
if (foodSpawned != foodPerLevel * level)
{
foods[foods.length] = new Food(foodData);
foodSpawned++;
foodOnScreen++;
}
else
{
if (foodOnScreen == 0)
return false;
}
return true;
}
window.onload = function()
{
Hunger.run();
}