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
18 changes: 17 additions & 1 deletion assets/js/platformer3x/Character.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Character extends GameObject {
// gravity for character enabled by default
this.gravityEnabled = true;
this.onTop = false;

// Velocity
this.yv = 0;
}

setSpriteAnimation(animation) {
Expand Down Expand Up @@ -133,11 +136,24 @@ class Character extends GameObject {
/**
* Update the y posiion and update y related states
*/
colY() {
while (!this.bottom > this.y) {
if (this.yv < 0) {
this.yv += 1;
} else {
this.yv -= 1;
}
}
}

updateY() {
if (this.bottom > this.y && this.gravityEnabled) {
this.y += GameEnv.gravity;
this.yv += GameEnv.gravity;
this.yv *= 0.9;
this.onTop = false;
} else {
this.colY();
this.yv = 0;
this.onTop = true;
}
}
Expand Down
52 changes: 40 additions & 12 deletions assets/js/platformer3x/PlayerBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export class PlayerBase extends Character {
// Add event listeners
document.addEventListener('keydown', this.keydownListener);
document.addEventListener('keyup', this.keyupListener);

// Velocity
this.xv = 0;
this.yv = 0;
}

/**
Expand Down Expand Up @@ -103,11 +107,6 @@ export class PlayerBase extends Character {
/**
* gameLoop helper: Udate Player jump height
*/
updateJump() {
// Jump height is 35% of the screen bottom, same as screen height
this.setY(this.y - (this.bottom * 0.35));
}

/**
* gameLoop: updates the player's movement based on the player's animation (idle, walk, run, jump, etc.)
*/
Expand All @@ -120,7 +119,7 @@ export class PlayerBase extends Character {
if (this.state.movement.up && !this.state.movement.falling) {
// jump
GameEnv.playSound("PlayerJump");
this.updateJump();
this.yv = GameEnv.bottom * -0.06;
// start falling
this.state.movement.falling = true;
}
Expand All @@ -129,16 +128,33 @@ export class PlayerBase extends Character {
// Player is moving left
if (this.state.direction === 'left' && this.state.movement.left && 'a' in this.pressedKeys) {
// Decrease the player's x position according to run or walk animation and related speed
this.setX(this.x - (this.state.animation === 'run' ? this.runSpeed : this.speed));
if (this.state.animation === 'run') {
this.xv -= this.runSpeed;
} else {
this.xv -= this.speed;
}
// Player is moving right
} else if (this.state.direction === 'right' && this.state.movement.right && 'd' in this.pressedKeys){
// Increase the player's x position according to run or walk animation and related speed
this.setX(this.x + (this.state.animation === 'run' ? this.runSpeed : this.speed));
if (this.state.animation === 'run') {
this.xv += this.runSpeed;
} else {
this.xv += this.speed;
}
}
}
GameEnv.PlayerPosition.playerX = this.x
GameEnv.PlayerPosition.playerY = this.y

// Update X
this.xv *= 0.8;
this.x += this.xv;
this.setX(this.x);

// Update Y
this.y += this.yv;
this.setY(this.y);

GameEnv.PlayerPosition.playerX = this.x;
GameEnv.PlayerPosition.playerY = this.y;
}

/**
Expand Down Expand Up @@ -335,16 +351,20 @@ export class PlayerBase extends Character {
if (this.collisionData.touchPoints.this.onTopofOther) {
this.state.movement = { up: false, down: false, left: true, right: true, falling: false};
this.gravityEnabled = false;
this.yv = 0;
this.y -= 1;

// Player is touching the wall with right side
} else if (this.collisionData.touchPoints.this.right) {
this.state.movement = { up: false, down: false, left: true, right: false, falling: false};
this.y -= 4;
this.x -= this.xv;
this.xv *= -1;

// Player is touching the wall with left side
} else if (this.collisionData.touchPoints.this.left) {
this.state.movement = { up: false, down: false, left: false, right: true, falling: false};
this.y -= 4;
this.x -= this.xv;
this.xv *= -1.5;
}
break;

Expand All @@ -354,12 +374,18 @@ export class PlayerBase extends Character {
if (this.collisionData.touchPoints.this.top && this.collisionData.touchPoints.other.bottom) {
this.state.movement = { up: false, down: false, left: true, right: true, falling: false};
this.gravityEnabled = false;
this.y -= this.yv;
this.yv = 0;
// Player is touching the wall with right side
} else if (this.collisionData.touchPoints.this.right) {
this.state.movement = { up: false, down: false, left: true, right: false, falling: false};
this.x -= this.xv;
this.xv *= -1;
// Player is touching the wall with left side
} else if (this.collisionData.touchPoints.this.left) {
this.state.movement = { up: false, down: false, left: false, right: true, falling: false};
this.x -= this.xv;
this.xv *= -1;
}
break;

Expand All @@ -369,6 +395,8 @@ export class PlayerBase extends Character {
// Player is on the floor
if (this.onTop) {
this.state.movement = { up: false, down: false, left: true, right: true, falling: false};
this.y -= this.yv;
this.yv = 0;
// Player is falling, there are no collisions, but is in default state
} else {
this.state.movement = { up: false, down: false, left: true, right: true, falling: true};
Expand Down
14 changes: 8 additions & 6 deletions assets/js/platformer3x/PlayerBoss.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ export class PlayerBoss extends PlayerBase {
updateJump() {
let jumpHeightFactor;
if (GameEnv.difficulty === "easy") {
jumpHeightFactor = 0.50;
jumpHeightFactor = 0.05;
} else if (GameEnv.difficulty === "normal") {
jumpHeightFactor = 0.40;
jumpHeightFactor = 0.04;
}
if (GameEnv.currentLevel.tag == "boss") {
jumpHeightFactor = 0.50;
jumpHeightFactor = 0.05;
}
this.setY(this.y - (this.bottom * jumpHeightFactor));
this.yv = -this.bottom * jumpHeightFactor;
this.y += this.yv;
this.setY(this.y);
}
/**
* @override
Expand Down Expand Up @@ -123,11 +125,11 @@ export class PlayerBoss extends PlayerBase {
// GoombaBounce deals with player.js and goomba.js
if (GameEnv.goombaBounce === true) {
GameEnv.goombaBounce = false;
this.y = this.y - 100;
this.y = -10;
}
if (GameEnv.goombaBounce1 === true) {
GameEnv.goombaBounce1 = false;
this.y = this.y - 250
this.yv = -25;
}
// 2. Player touches goomba sides of goomba
} else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) {
Expand Down
15 changes: 9 additions & 6 deletions assets/js/platformer3x/PlayerGreece.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@ export class PlayerGreece extends PlayerBase {
* @override
* gameLoop helper: Update Player jump height, replaces PlayerBase updateJump using settings from GameEnv
*/
updateJump() {
updateJump() {
let jumpHeightFactor;
if (GameEnv.difficulty === "easy") {
jumpHeightFactor = 0.50;
jumpHeightFactor = 0.05;
} else if (GameEnv.difficulty === "normal") {
jumpHeightFactor = 0.40;
} else {
jumpHeightFactor = 0.30;
jumpHeightFactor = 0.04;
}
this.setY(this.y - (this.bottom * jumpHeightFactor));
if(GameEnv.currentLevel.tag == "boss"){
jumpHeightFactor = 0;
}
this.yv = -this.bottom * jumpHeightFactor
this.y += this.yv;
this.setY(this.y);
}

/**
Expand Down
16 changes: 9 additions & 7 deletions assets/js/platformer3x/PlayerHills.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ export class PlayerHills extends PlayerBase {
updateJump() {
let jumpHeightFactor;
if (GameEnv.difficulty === "easy") {
jumpHeightFactor = 0.50;
jumpHeightFactor = 0.05;
} else if (GameEnv.difficulty === "normal") {
jumpHeightFactor = 0.40;
jumpHeightFactor = 0.04;
}
if(GameEnv.currentLevel.tag == "boss"){
jumpHeightFactor = 0;
}
this.setY(this.y - (this.bottom * jumpHeightFactor));
this.yv = -this.bottom * jumpHeightFactor
this.y += this.yv;
this.setY(this.y);
}

/**
Expand Down Expand Up @@ -95,11 +97,11 @@ export class PlayerHills extends PlayerBase {
// GoombaBounce deals with player.js and goomba.js
if (GameEnv.goombaBounce === true) {
GameEnv.goombaBounce = false;
this.y = this.y - 100;
this.yv = -10;
}
if (GameEnv.goombaBounce1 === true) {
GameEnv.goombaBounce1 = false;
this.y = this.y - 250
this.yv = -25;
}
// 2. Player touches goomba sides of goomba
} else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) {
Expand Down Expand Up @@ -127,11 +129,11 @@ export class PlayerHills extends PlayerBase {
// GoombaBounce deals with player.js and goomba.js
if (GameEnv.goombaBounce === true) {
GameEnv.goombaBounce = false;
this.y = this.y - 100;
this.y = -10;
}
if (GameEnv.goombaBounce1 === true) {
GameEnv.goombaBounce1 = false;
this.y = this.y - 250
this.y = -25;
}
// 2. Player touches goomba sides of goomba
} else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) {
Expand Down
14 changes: 8 additions & 6 deletions assets/js/platformer3x/PlayerIce.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ export class PlayerIce extends PlayerBase {
updateJump() {
let jumpHeightFactor;
if (GameEnv.difficulty === "easy") {
jumpHeightFactor = 0.50;
jumpHeightFactor = 0.05;
} else if (GameEnv.difficulty === "normal") {
jumpHeightFactor = 0.40;
jumpHeightFactor = 0.04;
} else {
jumpHeightFactor = 0.30;
jumpHeightFactor = 0.03;
}
this.setY(this.y - (this.bottom * jumpHeightFactor));
this.yv = -this.bottom * jumpHeightFactor;
this.y += this.yv;
this.setY(this.y);
}
update(){
super.update();
Expand Down Expand Up @@ -103,11 +105,11 @@ export class PlayerIce extends PlayerBase {
// GoombaBounce deals with player.js and goomba.js
if (GameEnv.goombaBounce === true) {
GameEnv.goombaBounce = false;
this.y = this.y - 100;
this.y = -10;
}
if (GameEnv.goombaBounce1 === true) {
GameEnv.goombaBounce1 = false;
this.y = this.y - 250
this.y = -25;
}
// 2. Player touches goomba sides of goomba
} else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) {
Expand Down
14 changes: 8 additions & 6 deletions assets/js/platformer3x/PlayerMini.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ export class PlayerMini extends PlayerBase {
updateJump() {
let jumpHeightFactor;
if (GameEnv.difficulty === "easy") {
jumpHeightFactor = 0.50;
jumpHeightFactor = 0.05;
} else if (GameEnv.difficulty === "normal") {
jumpHeightFactor = 0.40;
jumpHeightFactor = 0.04;
} else {
jumpHeightFactor = 0.30;
jumpHeightFactor = 0.03;
}
this.setY(this.y - (this.bottom * jumpHeightFactor));
this.yv = -this.bottom * jumpHeightFactor;
this.y += this.yv;
this.setY(this.y);
}
update(){
super.update();
Expand Down Expand Up @@ -106,11 +108,11 @@ export class PlayerMini extends PlayerBase {
// GoombaBounce deals with player.js and goomba.js
if (GameEnv.goombaBounce === true) {
GameEnv.goombaBounce = false;
this.y = this.y - 100;
this.y = -10;
}
if (GameEnv.goombaBounce1 === true) {
GameEnv.goombaBounce1 = false;
this.y = this.y - 250
this.y = -25;
}
// 2. Player touches goomba sides of goomba
} else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) {
Expand Down
14 changes: 8 additions & 6 deletions assets/js/platformer3x/PlayerMiniHogwarts.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ export class PlayerMiniHogwarts extends PlayerBase {
updateJump() {
let jumpHeightFactor;
if (GameEnv.difficulty === "easy") {
jumpHeightFactor = 0.50;
jumpHeightFactor = 0.05;
} else if (GameEnv.difficulty === "normal") {
jumpHeightFactor = 0.40;
jumpHeightFactor = 0.04;
} else {
jumpHeightFactor = 0.30;
jumpHeightFactor = 0.03;
}
this.setY(this.y - (this.bottom * jumpHeightFactor));
this.yv = -this.bottom * jumpHeightFactor;
this.y += this.yv;
this.setY(this.y);
}
update(){
super.update();
Expand Down Expand Up @@ -107,11 +109,11 @@ export class PlayerMiniHogwarts extends PlayerBase {
// GoombaBounce deals with player.js and goomba.js
if (GameEnv.goombaBounce === true) {
GameEnv.goombaBounce = false;
this.y = this.y - 100;
this.y = -10;
}
if (GameEnv.goombaBounce1 === true) {
GameEnv.goombaBounce1 = false;
this.y = this.y - 250
this.y = -25;
}
// 2. Player touches goomba sides of goomba
} else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) {
Expand Down
Loading