Skip to content

Commit

Permalink
added game engine
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipNelson5 committed Aug 28, 2018
1 parent 2af0f6e commit 864a9cf
Show file tree
Hide file tree
Showing 21 changed files with 1,439 additions and 0 deletions.
82 changes: 82 additions & 0 deletions CS5410_GameDev/GameEngine/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!DOCTYPE HTML>
<html lang = "en-US">
<head>
<meta charset = "UTF-8">
<title>Breakout!</title>

<link rel = "stylesheet" type = "text/css" href = "styles/game.css">
</head>
<body onload = "Engine.game.initialize();">

<div id = "id-main-div">

<!-- Main Menu -->
<div id = "main-menu" class = "screen">
<h1>Main Menu</h1>
<ul class = "menu">
<li><button id = "id-new-game">New&nbsp;Game</button></li>
<li><button id = "id-high-scores">High&nbsp;Scores</button></li>
<li><button id = "id-help">Help</button></li>
<li><button id = "id-about">Credits</button></li>
</ul>
</div>

<!-- Game -->
<div id = "game-play" class = "screen">
<canvas id = "canvas-main"></canvas>
</div>

<!-- High Scores -->
<div id = "high-scores" class = "screen">
<h1>High Scores</h1>
<ol id = 'id-high-score-list'> </ol>
<ul class = "menu">
<li><button id = "id-high-scores-back">Back</button></li>
<li><button id = "id-clear-high-scores">Reset</button></li>
</ul>
</div>

<!-- Help -->
<div id = "help" class = "screen">
<h1>Help</h1>
<p>Use A / D or ◀ / ▶ to move the paddle </br>Q will quit the game at any time</p>
<ul class = "menu">
<li><button id = "id-help-back">Back</button></li>
</ul>
</div>

<!-- About -->
<div id = "about" class = "screen">
<h1>About</h1>
<p>Developed by</p>
<p>Philip Nelson</p>
<ul class = "menu">
<li><button id = "id-about-back">Back</button></li>
</ul>
</div>

</div> <!-- mainMenu -->

<!-- The Game Engine Objects -->
<script>
let Engine = {
screens : {},
}, gheight, gwidth;
</script>

<!-- All the modules -->
<script src = "scripts/input.js"></script>
<script src = "scripts/renderer.js"></script>
<script src = "scripts/game.js"></script>
<script src = "scripts/mainmenu.js"></script>
<script src = "scripts/movable.js"></script>
<script src = "scripts/particle-system.js"></script>
<script src = "scripts/random.js"></script>
<script src = "scripts/persistance.js"></script>
<script src = "scripts/gameplay.js"></script>
<script src = "scripts/highscores.js"></script>
<script src = "scripts/help.js"></script>
<script src = "scripts/about.js"></script>

</body>
</html>
19 changes: 19 additions & 0 deletions CS5410_GameDev/GameEngine/scripts/about.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Engine.screens['about'] = (function(game) {
'use strict';

function initialize() {
document.getElementById('id-about-back').addEventListener(
'click',
function() { game.showScreen('main-menu'); });
}

function run() {
//
// I know this is empty, there isn't anything to do.
}

return {
initialize : initialize,
run : run
};
}(Engine.game));
57 changes: 57 additions & 0 deletions CS5410_GameDev/GameEngine/scripts/game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// ------------------------------------------------------------------
//
// This is the game engine object.
// Everything about the game is an attribute of Engine
//
// ------------------------------------------------------------------

Engine.game = (function(screens) {
'use strict';

// -----------------------------------------------------------------
//
// showScreen is used to change to a new active screen.
//
// -----------------------------------------------------------------
function showScreen(id) {
let screen = 0;
let active = null;

// Remove the active state from all screens.
active = document.getElementsByClassName('active');
for (screen = 0; screen < active.length; screen++) {
active[screen].classList.remove('active');
}

// Tell the screen run
screens[id].run();

// Set the new screen active
document.getElementById(id).classList.add('active');
}

// -----------------------------------------------------------------
//
// Perform the one-time game initialization.
//
// -----------------------------------------------------------------
function initialize() {
let screen = null;

// Initialize all the screens
for (screen in screens) {
if (screens.hasOwnProperty(screen)) {
screens[screen].initialize();
}
}

// Make the main-menu screen active
showScreen('main-menu');
}

return {
initialize : initialize,
showScreen : showScreen
};

}(Engine.screens));
107 changes: 107 additions & 0 deletions CS5410_GameDev/GameEngine/scripts/gameplay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
Engine.screens['game-play'] = (function(game, graphics, input, movable, particles) {
'use strict';

let mouseCapture = false,
mouse = input.Mouse(),
keyboard = input.Keyboard(),
ship = null,
cancelNextRequest = false,
lastTimeStamp;

function quit() {
// cancel next render request
cancelNextRequest = true;
// return to the main menu
game.showScreen('main-menu');
}

function initialize() {
console.log('game initializing...');

graphics.initialize();

ship = graphics.Texture( {
image : 'textures/spaceship.png',
center : { x : 100, y : 100 },
width : 50, height : 50/.52715,
rotation : 0,
moveRate : 200, // pixels per second
rotateRate : 3.14159 // Radians per second
});

movable.makeMovable(ship);

// Create the keyboard input handler and register the keyboard commands
keyboard.registerCommand(KeyEvent.DOM_VK_A, ship.moveLeft);
keyboard.registerCommand(KeyEvent.DOM_VK_D, ship.moveRight);
keyboard.registerCommand(KeyEvent.DOM_VK_W, ship.moveUp);
keyboard.registerCommand(KeyEvent.DOM_VK_S, ship.moveDown);
keyboard.registerCommand(KeyEvent.DOM_VK_Q, ship.rotateLeft);
keyboard.registerCommand(KeyEvent.DOM_VK_E, ship.rotateRight);
keyboard.registerCommand(KeyEvent.DOM_VK_ESCAPE, quit)

mouse = input.Mouse();
mouse.registerCommand('mousedown', function(e) {
mouseCapture = true;
// ship.moveTo({x : e.clientX, y : e.clientY});
particles.gravity({
position : {x: e.clientX, y: e.clientY},
});
});

mouse.registerCommand('mouseup', function(e) {
mouseCapture = false;
});

mouse.registerCommand('mousemove', function(e) {
if (mouseCapture) {
// ship.moveTo({x : e.clientX, y : e.clientY});
particles.gravity({
position : {x: e.clientX, y: e.clientY},
});
}
});
}

function update(dt) {
keyboard.update(dt);
mouse.update(dt);
particles.update(dt);
}

function render() {
graphics.clear();
ship.draw();
particles.render();
}

//------------------------------------------------------------------
//
// This is the Game Loop function!
//
//------------------------------------------------------------------
function gameLoop(time) {

update(time - lastTimeStamp);
lastTimeStamp = time;

render();

if (!cancelNextRequest) {
requestAnimationFrame(gameLoop);
}
}

function run() {
lastTimeStamp = performance.now();

// Start the animation loop
cancelNextRequest = false;
requestAnimationFrame(gameLoop);
}

return {
initialize : initialize,
run : run
};
}(Engine.game, Engine.graphics, Engine.input, Engine.movable, Engine.ParticleSystem));
18 changes: 18 additions & 0 deletions CS5410_GameDev/GameEngine/scripts/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Engine.screens['help'] = (function(game) {
'use strict';

function initialize() {
document.getElementById('id-help-back').addEventListener(
'click',
function() { game.showScreen('main-menu'); });
}

function run() {
// there isn't anything to do.
}

return {
initialize : initialize,
run : run
};
}(Engine.game));
22 changes: 22 additions & 0 deletions CS5410_GameDev/GameEngine/scripts/highscores.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Engine.screens['high-scores'] = (function(game) {
'use strict';

function initialize() {
document.getElementById('id-high-scores-back').addEventListener(
'click',
function() { game.showScreen('main-menu'); });

document.getElementById('id-clear-high-scores').addEventListener(
'click',
function() { Engine.persistence.clear(); Engine.persistence.report() });
}

function run() {
Engine.persistence.report();
}

return {
initialize : initialize,
run : run
};
}(Engine.game));
Loading

0 comments on commit 864a9cf

Please sign in to comment.