Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<meta charset="UTF-8">
<title>jqueryBattle</title>
<link href="style.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="script.js"></script>
</head>

<body>
Expand Down
42 changes: 42 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ function update () {

// TODO: Collision detection & health adjustment

for(var i = 0; i < Bullets.length; i++) {
var bullet = bullets[i];
for(var j = 0; j < enemies; j++) {
var enemy = enemies[j];
if(isColliding(bullet, enemy)) {
enemy.health--;
bullet.health--;
}
}
}


// Player bounds checking
if (player.position.x < 0) {
Expand Down Expand Up @@ -151,10 +162,37 @@ function update () {
}

// TODO: Lose condition (i.e. if player is dead)
if(!cfg.isGameOver && player.health <= 0) {
player.element.remove();
cfg.isGameOver = true;
alert("Game Over!")
}

// TODO: Win condition (i.e. if all enemies are dead)
if(!cfg.isGameOver && enemies.length === 0) {
cfg.isGameOver = true;
alert("You win!");
}
}

function isColliding (e1, e2){
var e1left = e1.position.x - e1.width/2;
var e1right = e1.position.x + e1.width/2;
var e1top = e1.position.y + e1.height/2;
var e1bottom = e1.position.y - e1.height/2;

var e2left = e2.position.x - e2.width/2;
var e2right = e2.position.x + e2.width/2;
var e2top = e2.position.y + e2.height/2;
var e2bottom = e2.position.y - e2.height/2;

return !(
e1bottom > e2top ||
e1top < e2bottom ||
e1left > e2right ||
e1right < e2left
)
};
// Enemy attack movement
function runEnemyAI (enemies) {
var attackingEnemy;
Expand Down Expand Up @@ -201,6 +239,10 @@ function draw () {
}






// Input handling
$(document).bind('keydown keyup', function (evt) {
var direction = CODES[evt.which];
Expand Down
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ html, body, div, ul, li {
}

.bullet {
background-color: yellow;
background-color: blue;
}