Last active
June 7, 2017 11:57
-
-
Save snggeng/6c47b6e99aaa62984fa879f89d708489 to your computer and use it in GitHub Desktop.
Enemy object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Enemy = function(settings) { | |
// Settings | |
let enemyElement = null | |
// Prevent enemy from escaping the game window | |
function wall() { | |
const enemyRect = enemyElement.getBoundingClientRect() // get the active style values of our moving enemy | |
const w = parseInt(window.innerWidth) | |
const h = parseInt(window.innerHeight) | |
if (enemyRect.bottom > h) { | |
enemyElement.style.top = `${h - enemyRect.height}px` | |
} | |
if (enemyRect.top < 0) { | |
enemyElement.style.top = '0px' | |
} | |
if (enemyRect.left < 0) { | |
enemyElement.style.left = '0px' | |
} | |
if (enemyRect.right > w) { | |
enemyElement.style.left = `${w - enemyRect.width}px` | |
} | |
} | |
// Move the enemy around manually | |
function move(interactions) { | |
// Make enemy move around the game window | |
// Some function | |
// Keep enemy movement within our game window | |
if (settings.walls) { // if walls setting is true, enemy cannot move out of window | |
wall() | |
} | |
} | |
// Create the object asset | |
function create() { | |
enemyElement = document.createElement('div') | |
enemyElement.setAttribute('class', 'enemy') | |
enemyElement.style.top = '100px' | |
enemyElement.style.left = '100px' | |
enemyElement.style.height = '25px' | |
enemyElement.style.width = '25px' | |
enemyElement.style.borderRadius = '50%' | |
enemyElement.style.background = 'blue' | |
document.body.append(enemyElement) | |
} | |
function init() { | |
create() // create object | |
// add other functions to initialize | |
} | |
// render function needs to be accessed outside of the Player Object | |
// hence, this.render() | |
// so that when we call assets[i].render on it it'll render the move() function | |
// which belongs to the Player Object | |
this.render = function(interactions) { | |
move(interactions) | |
} | |
init() // initialize | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment