<commit_before>/**
 * @file app.js
 * @author <NAME> GitHub: klong
 * @decription Udacity Nanodegree Project 3 - HTML Canvas Arcade Game, all files in js directory are needed for game to function
 * see README.md file for more information about the game.
 * {@link http://klong.github.io// Game} - link to GitHub hosted game page
*/

/**
 * @class Enemy
 * @description creates an Enemy object the player must avoid
 * @param {num} startX - the horizontal start x location on canvas
 * @param {num} startY - the vertical start y location on canvas
 * @param {num} speed - the speed is number of pixels to move in x on an update
 * @param {string} sprite - the path to image file used for enemey
 * @param {num} xPivot - pixel value for horizontal pivot location
 * @param {num} yPivot - pixel value for vertical pivot location
 * @param {num} energyLevel - a normalised value to represent enemy 'energy level'
 */
var Enemy = function (startX, startY) {
    'use strict';
    // The image/sprite for our enemies, this uses a helper we've provided to easily load images
    // set x, y locations for coordinate sytem of game board
    // TODO: changing the coordinate system for the game boad is not currently working - set both to zero
    this.x = startX;
    this.y = startY;
    // initialise enemy speed
    this.speed = 100;
    // set the graphic for player
    this.sprite = 'images/enemy-bug.png';
    // define x and y pivot for 'center' of the graphics design
    // TODO: the pivot points are not referenced or used in any  of the game code yet
    this.xPivot = 50;
    this.yPivot = 90;
    // set enemy energy level
    //energyLevel will be a floating point value between 0 and 1
    this.energyLevel = 1;
};

// Update the enemy's position, required method for game
// Parameter: dt, a time delta between ticks
Enemy.prototype.update = function (dt) {
    'use strict';
    // NOTE: the 'dt' parameter ensures the game runs at same speed for all computers.
    this.move(dt);
    this.offGameBoard(); // check if enemy is off the tiles of the game board
    // TODO: not all of game functionality rewuired for enemy <-> player collisions is complete
    this.collidingWithPlayer();
    // TODO: not all of game functionality rewuired for enemy <-> player collisions is complete
    this.collidingWithTreasure();
};

Enemy.prototype.move = function (dt) {
     this.x += this.speed * dt;
};

Enemy.prototype.collidingWithPlayer = function () {
    'use strict';
    if  (player.x <= this.x + 50 &&
        this.x <= player.x + 20 &&
        player.y <= this.y + 20 &&
        this.y <= player.y + 20)
    {
        // when colliding with player, 'bump' them
        this.bump(player);
    }
};

Enemy.prototype.collidingWithTreasure = function () {
    'use strict';
    for (var i = 0; i < allTreasures.length; i++) {
        if  (allTreasures[i].x <= this.x + 50 &&
            this.x <= allTreasures[i].x + 20 &&
            allTreasures[i].y <= this.y + 20 &&
            this.y <= allTreasures[i].y + 20)
        {
            // when enemy is colliding with treasure object i in allTresures array
            // the enemies 'this.bump' will call 'bumped' method on the treasure object
            this.bump[allTreasures[i]];
        }
    }
};

Enemy.prototype.bump = function (targetObj) {
    // call the hit objects 'bumped' methood
    targetObj.bumped();
    // bumping something depletes some energy
    this.energyLevel -= 0.0005;
};

Enemy.prototype.offGameBoard = function () {
    'use strict';
    if (this.speed > 0 && this.x > gb.boardStartX + gb.gameBoardWidth ||
        this.speed < 0 && this.x < gb.boardStartX - gb.tileWidth)
        // re-position the enemy horizontaly off left of game board
        this.x = gb.boardStartX - (2 * gb.tileWidth);
        // set a new positive random speed for enemy
        this.randomSpeed(100, 200);
};


Enemy.prototype.randomSpeed = function(minSpeed, maxSpeed) {
    'use strict';
    // maxSpeed is multiplied by normalised factor of enemies energyLevel
    // TODO: the updating of normalised energyLevel does not have the required game rules yet
    this.speed = Math.floor((Math.random() * (maxSpeed * this.energyLevel)) + minSpeed);
};<commit_msg>last JSHint zap<commit_after>/**
 * @file app.js
 * @author <NAME> GitHub: klong
 * @decription Udacity Nanodegree Project 3 - HTML Canvas Arcade Game, all files in js directory are needed for game to function
 * see README.md file for more information about the game.
 * {@link http://klong.github.io// Game} - link to GitHub hosted game page
*/

/**
 * @class Enemy
 * @description creates an Enemy object the player must avoid
 * @param {num} startX - the horizontal start x location on canvas
 * @param {num} startY - the vertical start y location on canvas
 * @param {num} speed - the speed is number of pixels to move in x on an update
 * @param {string} sprite - the path to image file used for enemey
 * @param {num} xPivot - pixel value for horizontal pivot location
 * @param {num} yPivot - pixel value for vertical pivot location
 * @param {num} energyLevel - a normalised value to represent enemy 'energy level'
 */
var Enemy = function (startX, startY) {
    'use strict';
    // The image/sprite for our enemies, this uses a helper we've provided to easily load images
    // set x, y locations for coordinate sytem of game board
    // TODO: changing the coordinate system for the game boad is not currently working - set both to zero
    this.x = startX;
    this.y = startY;
    // initialise enemy speed
    this.speed = 100;
    // set the graphic for player
    this.sprite = 'images/enemy-bug.png';
    // define x and y pivot for 'center' of the graphics design
    // TODO: the pivot points are not referenced or used in any  of the game code yet
    this.xPivot = 50;
    this.yPivot = 90;
    // set enemy energy level
    //energyLevel will be a floating point value between 0 and 1
    this.energyLevel = 1;
};

// Update the enemy's position, required method for game
// Parameter: dt, a time delta between ticks
Enemy.prototype.update = function (dt) {
    'use strict';
    // NOTE: the 'dt' parameter ensures the game runs at same speed for all computers.
    this.move(dt);
    this.offGameBoard(); // check if enemy is off the tiles of the game board
    // TODO: not all of game functionality rewuired for enemy <-> player collisions is complete
    this.collidingWithPlayer();
    // TODO: not all of game functionality rewuired for enemy <-> player collisions is complete
    this.collidingWithTreasure();
};

Enemy.prototype.move = function (dt) {
     this.x += this.speed * dt;
};

Enemy.prototype.collidingWithPlayer = function () {
    'use strict';
    if  (player.x <= this.x + 50 &&
        this.x <= player.x + 20 &&
        player.y <= this.y + 20 &&
        this.y <= player.y + 20)
    {
        // when colliding with player, 'bump' them
        this.bump(player);
    }
};

Enemy.prototype.collidingWithTreasure = function () {
    'use strict';
    for (var i = 0; i < allTreasures.length; i++) {
        if  (allTreasures[i].x <= this.x + 50 &&
            this.x <= allTreasures[i].x + 20 &&
            allTreasures[i].y <= this.y + 20 &&
            this.y <= allTreasures[i].y + 20)
        {
            // when enemy is colliding with treasure object i in allTresures array
            // the enemies 'this.bump' will call 'bumped' method on the treasure object
            this.bump([allTreasures[i]]);
        }
    }
};

Enemy.prototype.bump = function (targetObj) {
    // call the hit objects 'bumped' methood
    targetObj.bumped();
    // bumping something depletes some energy
    this.energyLevel -= 0.0005;
};

Enemy.prototype.offGameBoard = function () {
    'use strict';
    if (this.speed > 0 && this.x > gb.boardStartX + gb.gameBoardWidth ||
        this.speed < 0 && this.x < gb.boardStartX - gb.tileWidth)
        // re-position the enemy horizontaly off left of game board
        this.x = gb.boardStartX - (2 * gb.tileWidth);
        // set a new positive random speed for enemy
        this.randomSpeed(100, 200);
};


Enemy.prototype.randomSpeed = function(minSpeed, maxSpeed) {
    'use strict';
    // maxSpeed is multiplied by normalised factor of enemies energyLevel
    // TODO: the updating of normalised energyLevel does not have the required game rules yet
    this.speed = Math.floor((Math.random() * (maxSpeed * this.energyLevel)) + minSpeed);
};
{'fraction_non_alphanumeric': 0.1058351051263879, 'fraction_numerical': 0.009922041105598866, 'mean_word_length': 2.6989951944080386, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5464665', 'n_tokens_mistral': 2591, 'n_tokens_neox': 2456, 'n_words': 972}