Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // increase for more difficult
- var difficulty = 2;
- function startGame() {
- //........................
- moveEnemy(difficulty);
- //........................
- function moveEnemy(difficulty) {
- var
- speed = getEnemyMoveSpeed(difficulty);
- //.........................
- }
- //..........................
- function getEnemyMoveSpeed(difficulty){
- var dx = enemy.x - disc.x + 2.5,
- dy = enemy.y - disc.y + 2.5,
- distance = Math.sqrt(dx * dx + dy * dy);
- distance = distance === 0 ? 0.1 : distance;
- return {
- x: Math.abs((dx / distance) * difficulty),
- y: Math.abs((dy / distance) * difficulty)
- }
- }
- //..........................
- }
- // -------------------------------------------------- old stuff
- var canvasDrawer = new CanvasDrawer(),
- participantRadius = 15,
- startPlayerX = 20,
- disc = new Disc(canvasDrawer.canvasWidth / 2, canvasDrawer.canvasHeight / 2, 12),
- player = new Player('Pesho', 20, 180, participantRadius),
- enemy = new Enemy('Gosho', canvasDrawer.canvasWidth - 20, canvasDrawer.canvasHeight / 2, participantRadius);
- function startGame() {
- canvasDrawer.clear();
- canvasDrawer.drawParticipant(player, "red");
- canvasDrawer.drawParticipant(enemy, "blue");
- canvasDrawer.drawDisc(disc);
- player.move();
- disc.move();
- detectCollisionWithDisc(player, disc);
- detectCollisionWithDisc(enemy, disc);
- detectDiscCollisionWithWalls(disc, canvasDrawer);
- moveEnemy();
- function detectCollisionWithDisc(player, disc) {
- var dx = player.x - disc.x,
- dy = player.y - disc.y,
- distance = Math.sqrt(dx * dx + dy * dy);
- if (distance < player.radius + disc.radius) {
- if (distance === 0) {
- distance = 0.1;
- }
- var unitX = dx / distance,
- unitY = dy / distance,
- force = -2,
- forceX = unitX * force,
- forceY = unitY * force;
- disc.velocity.x += forceX;
- disc.velocity.y += forceY;
- return true;
- } else {
- return false;
- }
- }
- function detectDiscCollisionWithWalls(disc, canvasDrawer) {
- // bounce off the floor
- if (disc.y > canvasDrawer.canvasHeight - disc.radius) {
- disc.y = canvasDrawer.canvasHeight - disc.radius;
- disc.velocity.y = -Math.abs(disc.velocity.y);
- }
- // bounce off ceiling
- if (disc.y < disc.radius + 0) {
- disc.y = disc.radius + 0;
- disc.velocity.y = Math.abs(disc.velocity.y);
- }
- // bounce off right wall
- if (disc.x > canvasDrawer.canvasWidth - disc.radius) {
- disc.x = canvasDrawer.canvasWidth - disc.radius;
- disc.velocity.x = -Math.abs(disc.velocity.x);
- }
- // bounce off left wall
- if (disc.x < disc.radius) {
- disc.x = disc.radius;
- disc.velocity.x = Math.abs(disc.velocity.x);
- }
- }
- // enemy movement
- function moveEnemy() {
- var playGroundMiddle = canvasDrawer.canvasWidth / 2,
- enemyDefenceLine = canvasDrawer.canvasWidth * 5 / 8,
- speed = getEnemyMoveSpeed();
- if (disc.x > canvasDrawer.canvasWidth / 2) {
- // if disc is passed defence line but not enemy x
- if (disc.x > enemyDefenceLine && enemy.x > enemyDefenceLine) {
- //enemy.move("left", speed.x);
- moveEnemyX(speed.x);
- moveEnemyY(speed.y);
- }
- moveEnemyY(speed.y);
- } else {
- enemy.reset();
- }
- }
- function moveEnemyY(speed){
- if (disc.y < enemy.y && disc.x < enemy.x) {
- enemy.move("up", speed);
- } else if (disc.y > enemy.y && disc.x < enemy.x) {
- enemy.move("down", speed);
- }
- }
- function moveEnemyX(speed) {
- // if disc is passed enemy x
- if (disc.x >= enemy.x) {
- enemy.reset();
- } else {
- enemy.move("left", speed);
- }
- }
- // TODO
- function getEnemyMoveSpeed(){
- return {
- x: 2.5,
- y: 2.5
- }
- }
- requestAnimationFrame(function () {
- startGame();
- });
- }
- startGame();
- // ENEMY
- var Enemy = (function (parent) {
- Enemy.extends(parent);
- function Enemy(name, x, y, radius) {
- parent.call(this, name, x, y, radius);
- //this.imagePath = './Images/enemy.png'; //imagePath
- this.velocity = {
- x: 0,
- y: 0
- },
- this.startPosition = {
- 'x': x,
- 'y': y
- }
- }
- Enemy.prototype.move = function (direction, speed) {
- switch(direction) {
- case "left":
- this.x -= speed - this.velocity.x;
- break;
- case "right":
- this.x += speed + this.velocity.x;
- break;
- case "up":
- this.y -= speed - this.velocity.y;
- break;
- case "down":
- this.y += speed + this.velocity.y;
- break;
- }
- }
- Enemy.prototype.reset = function() {
- var resetSpeed = getEnemyResetSpeed(this, this.startPosition);
- resetEnemyX(this, resetSpeed, this.startPosition);
- if (this.y > this.startPosition.y) {
- resetEnemyY(this, resetSpeed, this.startPosition, -1);
- } else {
- resetEnemyY(this, resetSpeed, this.startPosition);
- }
- }
- // reset helper functions
- function resetEnemyX(enemy, resetSpeed, startPosition) {
- var enemyX = enemy.x + resetSpeed.x;
- enemy.x = enemyX > startPosition.x ? startPosition.x : enemyX;
- }
- function resetEnemyY(enemy, resetSpeed, startPosition, direction) {
- var direction = direction || 1,
- enemyY = enemy.y + resetSpeed.y * direction;
- if (direction < 0){
- enemy.y = enemyY < startPosition.y ? startPosition.y : enemyY;
- } else {
- enemy.y = enemyY > startPosition.y ? startPosition.y : enemyY;
- }
- }
- function getEnemyResetSpeed(enemy, startPosition) {
- var dx = enemy.x - startPosition.x,
- dy = enemy.y - startPosition.y,
- distance = Math.sqrt(dx * dx + dy * dy);
- distance = distance === 0 ? distance = 0.1 : distance;
- return {
- x: Math.abs((dx / distance) * 2),
- y: Math.abs((dy / distance) * 2)
- }
- }
- return Enemy;
- }(Participant));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement