From 0593b39a56a7de6c0aefe4b214a19eb8cb6bc139 Mon Sep 17 00:00:00 2001 From: kdrzazga Date: Thu, 19 Dec 2024 18:12:29 +0100 Subject: [PATCH] Rats pushing --- other/electrician/files/classdef.js | 51 +++++++++++++++++++++----- other/electrician/files/electrician.js | 13 +++++-- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/other/electrician/files/classdef.js b/other/electrician/files/classdef.js index dc63e05..60bc42c 100644 --- a/other/electrician/files/classdef.js +++ b/other/electrician/files/classdef.js @@ -79,6 +79,7 @@ class Building { this.ladder = new Ladder(); this.floors = []; this.wires = []; + this.enemies = []; this.leftPowerLine = new PowerLine(); this.rightPowerLine = new PowerLine(); } @@ -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 }; } @@ -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; } diff --git a/other/electrician/files/electrician.js b/other/electrician/files/electrician.js index 3d116a5..9b8c80a 100644 --- a/other/electrician/files/electrician.js +++ b/other/electrician/files/electrician.js @@ -40,8 +40,9 @@ class MainScene extends Phaser.Scene { update() { this.handlePlayerMovement(); - this.conditionalFallDown(); this.handleEnemyMovement(); + this.checkCollisions(); + this.conditionalFallDown(); } jump(direction) { @@ -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 = "" @@ -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(){