Skip to content

Commit

Permalink
Rats pushing
Browse files Browse the repository at this point in the history
  • Loading branch information
kdrzazga committed Dec 19, 2024
1 parent dc3e1bc commit 0593b39
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
51 changes: 41 additions & 10 deletions other/electrician/files/classdef.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class Building {
this.ladder = new Ladder();
this.floors = [];
this.wires = [];
this.enemies = [];
this.leftPowerLine = new PowerLine();
this.rightPowerLine = new PowerLine();
}
Expand Down Expand Up @@ -237,16 +238,44 @@ class Wire {
}
}

class Rat {
class Enemy {
constructor(id){
this.sprite = null;
this.id = id;
this.speed =1;
this.active = true;
this.speed = 1;
this.minX = 150;
this.maxX = Floor.WIDTH;
}

collide(player) {
const distanceX = player.x - this.sprite.x;
const distanceY = player.y - this.sprite.y;

const collisionVertical = -40 < distanceY && distanceY < 0;

if (!collisionVertical) return 0;

const threshold = 20;
if (Math.abs(distanceX) < threshold) {
if (distanceX < 0) {
return -1;
} else {
return 1;
}
}

return 0;
}
}

class Rat extends Enemy{
constructor(id){
super(id);
}

init(physics, y){
this.sprite = physics.add.sprite(180 + (this.id + 1)*110, y, 'rat' + this.id);
this.minX = 150;
this.maxX = Floor.WIDTH;
this.sprite.velocity = { x: this.speed };
}

Expand All @@ -264,12 +293,14 @@ class Creator{
building.init(3, physics);
building.includeWiresInInfoFrame();

building.rat1 = new Rat(1);
building.rat1.init(physics, 589);
building.rat2 = new Rat(2);
building.rat2.init(physics, 104);
building.rat2.minX = 2 * 6 * Wire.SIZE;
building.rat2.maxX = (6 + 22) * Wire.SIZE;
const rat1 = new Rat(1);
rat1.init(physics, 589);
const rat2 = new Rat(2);
rat2.init(physics, 104);
rat2.minX = 2 * 6 * Wire.SIZE;
rat2.maxX = (6 + 22) * Wire.SIZE;

building.enemies.push(rat1, rat2);

return building;
}
Expand Down
13 changes: 10 additions & 3 deletions other/electrician/files/electrician.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ class MainScene extends Phaser.Scene {

update() {
this.handlePlayerMovement();
this.conditionalFallDown();
this.handleEnemyMovement();
this.checkCollisions();
this.conditionalFallDown();
}

jump(direction) {
Expand Down Expand Up @@ -83,6 +84,13 @@ class MainScene extends Phaser.Scene {
}
}

checkCollisions(){
const collidingEnemy = this.building.enemies.find(e => e.collide(this.player) != 0);

if (collidingEnemy != null)
this.player.x += 15 * collidingEnemy.collide(this.player);
}

conditionalFallDown(){
let flrs = ""

Expand Down Expand Up @@ -150,8 +158,7 @@ class MainScene extends Phaser.Scene {
}

handleEnemyMovement(){
this.building.rat1.move();
this.building.rat2.move();
this.building.enemies.forEach(enemy => enemy.move());
}

writeFloorInfo(){
Expand Down

0 comments on commit 0593b39

Please sign in to comment.