From 35d4a63fafed9d3e088fe1b2f9f8bd9ce085f873 Mon Sep 17 00:00:00 2001 From: robschroeder333 Date: Tue, 21 Sep 2021 14:38:47 -0700 Subject: [PATCH 1/6] merge --- objects.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/objects.js b/objects.js index 0494d57..c162386 100644 --- a/objects.js +++ b/objects.js @@ -69,6 +69,7 @@ let player = { drillStrength: 3, shotDelay: 1, direction: 'right', + stun: //bullets, //lastShot, move: function() { @@ -138,6 +139,7 @@ let player = { let checkRow let checkBlock + //level collision //collision right if (this.velocity.x > 0) { nextX = this.origin.x + this.size.x + this.velocity.x @@ -250,6 +252,33 @@ let player = { } } } + + //enemy collision + let e = enemies.head + let world = {x: this.origin.x - level.origin.x, + y: this.origin.y - level.origin.y} + let nextPos = new CollisionObj({x: world.x + this.velocity.x, + y: world.y + this.velocity.y}, + {x: world.x + this.velocity.x + this.size.x, + y: world.y + this.velocity.y + this.size.y}) + let collision = false + while (e !== null) { + if (isColliding(new CollisionObj(e.origin, {x: e.origin.x + e.size.x, + y: e.origin.y + e.size.y}), + nextPos)) { + collision = true + } + e = e.next + } + if (collision) { + console.log('collision') + this.health -= 1 + this.velocity.x *= -3 + this.velocity.y *= -3 + } + + console.log(this.velocity.x, ', ', this.velocity.y) + //move player this.origin.x += this.velocity.x this.origin.y += this.velocity.y }, From 956fba734d739a39d818f213e22fd2823904ee35 Mon Sep 17 00:00:00 2001 From: robschroeder333 Date: Tue, 21 Sep 2021 15:11:33 -0700 Subject: [PATCH 2/6] feat: add initial player to enemy collision detection --- objects.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/objects.js b/objects.js index c162386..5905eeb 100644 --- a/objects.js +++ b/objects.js @@ -69,7 +69,7 @@ let player = { drillStrength: 3, shotDelay: 1, direction: 'right', - stun: + stun: 0, //bullets, //lastShot, move: function() { @@ -271,13 +271,13 @@ let player = { e = e.next } if (collision) { - console.log('collision') + // console.log('collision') this.health -= 1 this.velocity.x *= -3 this.velocity.y *= -3 } - console.log(this.velocity.x, ', ', this.velocity.y) + // console.log(this.velocity.x, ', ', this.velocity.y) //move player this.origin.x += this.velocity.x this.origin.y += this.velocity.y From d0ff7cf53c43dac2ce8e1fb053234cef95b64fef Mon Sep 17 00:00:00 2001 From: robschroeder333 Date: Mon, 27 Sep 2021 15:07:38 -0700 Subject: [PATCH 3/6] add pushback when player collides with enemy and hitStun --- logic.js | 1 + objects.js | 43 +++++++++++++++++++++++++++++++------------ utility.js | 13 +++++++++---- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/logic.js b/logic.js index efa6bad..2eeb38a 100644 --- a/logic.js +++ b/logic.js @@ -158,6 +158,7 @@ function update() { window.requestAnimationFrame(update) } +//player input function handleInput() { if (iLeft) { hor = -1 diff --git a/objects.js b/objects.js index 5905eeb..29c6055 100644 --- a/objects.js +++ b/objects.js @@ -67,13 +67,15 @@ let player = { drill: false, drillCount: 3, drillStrength: 3, - shotDelay: 1, + shotDelay: 1,//seconds + hitStunLength: .1,//seconds direction: 'right', - stun: 0, + isHit: false, + //hitStun, //bullets, //lastShot, move: function() { - + //keep track for shot direction if (iRight) { this.direction = 'right' } @@ -83,8 +85,7 @@ let player = { if (iUp) { this.direction = 'up' } - - + //drill ability if (!this.isGrounded && iDown && this.drillCount > 0) { this.drill = true } else { @@ -119,6 +120,13 @@ let player = { this.hSpeed = Math.clamp(this.hSpeed + 20, 10, this.speed) this.vSpeed = Math.clamp(this.vSpeed + 20, 10, this.speed) this.g = Math.clamp(this.g * 2, 100, 1000) + + //disable input during hitstun + if (this.hitStun != undefined && !delayCheck(this.hitStun, this.hitStunLength)) { + console.log('input disabled') + hor = 0 + ver = 0 + } //next position (before collision) this.velocity.x = Math.clamp((this.velocity.x + (hor * this.hSpeed)) * delta, -this.hMax, this.hMax) @@ -261,20 +269,31 @@ let player = { y: world.y + this.velocity.y}, {x: world.x + this.velocity.x + this.size.x, y: world.y + this.velocity.y + this.size.y}) - let collision = false + this.isHit = false while (e !== null) { if (isColliding(new CollisionObj(e.origin, {x: e.origin.x + e.size.x, y: e.origin.y + e.size.y}), nextPos)) { - collision = true + this.isHit = true } e = e.next } - if (collision) { - // console.log('collision') - this.health -= 1 - this.velocity.x *= -3 - this.velocity.y *= -3 + if (this.isHit) { + // console.log('hit') + if (this.hitstun == undefined || delayCheck(this.hitStun, this.hitStunLength)) { + this.hitStun = Date.now() + this.health -= 1 + if (this.velocity.x < 1 && this.velocity.x > -1) { + if (this.velocity.x >= 0) { + this.velocity.x += 10 + } else { + this.velocity.x -= 10 + } + } + this.velocity.x *= -3 + + this.velocity.y *= -2 + } } // console.log(this.velocity.x, ', ', this.velocity.y) diff --git a/utility.js b/utility.js index d999528..5325ad1 100644 --- a/utility.js +++ b/utility.js @@ -4,7 +4,6 @@ Math.clamp = function(number, min, max) { return Math.max(min, Math.min(number, max)); } - function drawObject(obj, color) { ctx.save() ctx.translate(obj.origin.x + level.origin.x, obj.origin.y + level.origin.y) @@ -12,10 +11,16 @@ function drawObject(obj, color) { ctx.fillRect(0, 0, obj.size.x, obj.size.y) ctx.restore() } - +function delayCheck(last, delay) { + if ((time - last)/1000 >= delay) { + return true + } else { + return false + } +} function canFire(obj) { - if ((obj.bullets.head == null && ((time - obj.lastShot)/1000 >= obj.shotDelay / 2)) - || (time - obj.lastShot)/1000 >= obj.shotDelay) { + if ((obj.bullets.head == null && delayCheck(obj.lastShot, obj.shotDelay)) + || delayCheck(obj.lastShot, obj.shotDelay)) { return true } else { return false From c314bcf7ce2d5ffe62fa792c0e7586988d6ed63e Mon Sep 17 00:00:00 2001 From: robschroeder333 Date: Thu, 30 Sep 2021 12:01:24 -0700 Subject: [PATCH 4/6] feat: adjust and debug pushback --- objects.js | 58 ++++++++++++++++++++++++++++++------------------------ utility.js | 6 +++--- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/objects.js b/objects.js index 29c6055..ef3c24a 100644 --- a/objects.js +++ b/objects.js @@ -68,7 +68,7 @@ let player = { drillCount: 3, drillStrength: 3, shotDelay: 1,//seconds - hitStunLength: .1,//seconds + hitStunLength: .3,//seconds direction: 'right', isHit: false, //hitStun, @@ -122,7 +122,7 @@ let player = { this.g = Math.clamp(this.g * 2, 100, 1000) //disable input during hitstun - if (this.hitStun != undefined && !delayCheck(this.hitStun, this.hitStunLength)) { + if (this.hitStun != undefined && !isDelayFinished(this.hitStun, this.hitStunLength)) { console.log('input disabled') hor = 0 ver = 0 @@ -132,14 +132,36 @@ let player = { this.velocity.x = Math.clamp((this.velocity.x + (hor * this.hSpeed)) * delta, -this.hMax, this.hMax) this.velocity.y = Math.clamp(((this.velocity.y + (ver * this.vSpeed) + this.g) * delta), -this.vMax, this.vMax) - //adds responsiveness to controls - if ((this.velocity.x > 0 && hor < 0) || (this.velocity.x < 0 && hor > 0)) { - this.velocity.x = 0 - this.hSpeed = 50 + if (this.hitStun == undefined || isDelayFinished(this.hitStun, this.hitStunLength)) { + + //adds responsiveness to controls + if ((this.velocity.x > 0 && hor < 0) || (this.velocity.x < 0 && hor > 0)) { + this.velocity.x = 0 + this.hSpeed = 50 + } + if (hor === 0 && Math.abs(this.velocity.x) < 0.01) { + this.velocity.x = 0 + this.hSpeed = 50 + } } - if (hor === 0 && Math.abs(this.velocity.x) < 0.01) { - this.velocity.x = 0 - this.hSpeed = 50 + + if (this.isHit) { + // console.log('hit') + if (this.hitstun == undefined || isDelayFinished(this.hitStun, this.hitStunLength)) { + this.hitStun = Date.now() + this.health -= 1 + if (this.velocity.x < 1 && this.velocity.x > -1) { + if (this.velocity.x >= 0) { + this.velocity.x += 20 + } else { + this.velocity.x -= 20 + } + } else { + this.velocity.x *= -3 + } + + this.velocity.y *= -2 + } } let nextX @@ -278,23 +300,7 @@ let player = { } e = e.next } - if (this.isHit) { - // console.log('hit') - if (this.hitstun == undefined || delayCheck(this.hitStun, this.hitStunLength)) { - this.hitStun = Date.now() - this.health -= 1 - if (this.velocity.x < 1 && this.velocity.x > -1) { - if (this.velocity.x >= 0) { - this.velocity.x += 10 - } else { - this.velocity.x -= 10 - } - } - this.velocity.x *= -3 - - this.velocity.y *= -2 - } - } + // console.log(this.velocity.x, ', ', this.velocity.y) //move player diff --git a/utility.js b/utility.js index 5325ad1..ec1ba40 100644 --- a/utility.js +++ b/utility.js @@ -11,7 +11,7 @@ function drawObject(obj, color) { ctx.fillRect(0, 0, obj.size.x, obj.size.y) ctx.restore() } -function delayCheck(last, delay) { +function isDelayFinished(last, delay) { if ((time - last)/1000 >= delay) { return true } else { @@ -19,8 +19,8 @@ function delayCheck(last, delay) { } } function canFire(obj) { - if ((obj.bullets.head == null && delayCheck(obj.lastShot, obj.shotDelay)) - || delayCheck(obj.lastShot, obj.shotDelay)) { + if ((obj.bullets.head == null && isDelayFinished(obj.lastShot, obj.shotDelay / 2)) + || isDelayFinished(obj.lastShot, obj.shotDelay)) { return true } else { return false From 4225084136c64c1fcc488a245e7ef480d820cd6a Mon Sep 17 00:00:00 2001 From: robschroeder333 Date: Thu, 30 Sep 2021 12:14:56 -0700 Subject: [PATCH 5/6] chore: comment and tidy code --- objects.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/objects.js b/objects.js index ef3c24a..f5a4076 100644 --- a/objects.js +++ b/objects.js @@ -85,6 +85,7 @@ let player = { if (iUp) { this.direction = 'up' } + //drill ability if (!this.isGrounded && iDown && this.drillCount > 0) { this.drill = true @@ -132,6 +133,7 @@ let player = { this.velocity.x = Math.clamp((this.velocity.x + (hor * this.hSpeed)) * delta, -this.hMax, this.hMax) this.velocity.y = Math.clamp(((this.velocity.y + (ver * this.vSpeed) + this.g) * delta), -this.vMax, this.vMax) + //disable control adjustment when stunned if (this.hitStun == undefined || isDelayFinished(this.hitStun, this.hitStunLength)) { //adds responsiveness to controls @@ -145,6 +147,7 @@ let player = { } } + //hitstun logic (pushback and damage) if (this.isHit) { // console.log('hit') if (this.hitstun == undefined || isDelayFinished(this.hitStun, this.hitStunLength)) { @@ -300,15 +303,13 @@ let player = { } e = e.next } - - // console.log(this.velocity.x, ', ', this.velocity.y) //move player this.origin.x += this.velocity.x this.origin.y += this.velocity.y }, fire: function() { -//spawn bullet in origin determined by direction and pass speed params accordingly + //spawn bullet in origin determined by direction and pass speed params accordingly switch (this.direction) { case 'right': this.bullets.add(new Bullet( From ac6dab8f41bacb1fe297bcb2588c5fbb488e1faf Mon Sep 17 00:00:00 2001 From: robschroeder333 Date: Thu, 30 Sep 2021 15:08:33 -0700 Subject: [PATCH 6/6] fix: adjusting movement speed and adding drill damage --- logic.js | 2 +- objects.js | 45 ++++++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/logic.js b/logic.js index 2eeb38a..d200394 100644 --- a/logic.js +++ b/logic.js @@ -86,7 +86,7 @@ function gameStart() { player.lastShot = Date.now() enemies = new Collection() - enemies.add(new Shooter({x: 400, y: 200}, {x: 15, y: 20}, 1)) //test enemy + enemies.add(new Shooter({x: 400, y: 200}, {x: 15, y: 20}, 2)) //test enemy document.addEventListener('keydown', (event) => { switch (event.code) { diff --git a/objects.js b/objects.js index f5a4076..ac24cf4 100644 --- a/objects.js +++ b/objects.js @@ -67,6 +67,7 @@ let player = { drill: false, drillCount: 3, drillStrength: 3, + drillSpeed: 10, shotDelay: 1,//seconds hitStunLength: .3,//seconds direction: 'right', @@ -85,7 +86,10 @@ let player = { if (iUp) { this.direction = 'up' } - + + if (this.isGrounded) { + this.g = 200 + } //drill ability if (!this.isGrounded && iDown && this.drillCount > 0) { this.drill = true @@ -120,24 +124,24 @@ let player = { this.hSpeed = Math.clamp(this.hSpeed + 20, 10, this.speed) this.vSpeed = Math.clamp(this.vSpeed + 20, 10, this.speed) - this.g = Math.clamp(this.g * 2, 100, 1000) - + this.g = Math.clamp(Math.pow(this.g, 1.25), 2, 1000) + //disable input during hitstun if (this.hitStun != undefined && !isDelayFinished(this.hitStun, this.hitStunLength)) { console.log('input disabled') hor = 0 ver = 0 } - + let vCap = (this.drill) ? this.vMax : this.vMax - this.drillSpeed //next position (before collision) this.velocity.x = Math.clamp((this.velocity.x + (hor * this.hSpeed)) * delta, -this.hMax, this.hMax) - this.velocity.y = Math.clamp(((this.velocity.y + (ver * this.vSpeed) + this.g) * delta), -this.vMax, this.vMax) + this.velocity.y = Math.clamp(((this.velocity.y + (ver * this.vSpeed) + this.g) * delta), -this.vMax, vCap) //disable control adjustment when stunned if (this.hitStun == undefined || isDelayFinished(this.hitStun, this.hitStunLength)) { //adds responsiveness to controls - if ((this.velocity.x > 0 && hor < 0) || (this.velocity.x < 0 && hor > 0)) { + if ((this.velocity.x > 0 && hor < 0) || (this.velocity.x < 0 && hor > 0)) { //never triggering this.velocity.x = 0 this.hSpeed = 50 } @@ -151,19 +155,25 @@ let player = { if (this.isHit) { // console.log('hit') if (this.hitstun == undefined || isDelayFinished(this.hitStun, this.hitStunLength)) { - this.hitStun = Date.now() - this.health -= 1 - if (this.velocity.x < 1 && this.velocity.x > -1) { - if (this.velocity.x >= 0) { - this.velocity.x += 20 + this.hitStun = Date.now() + + if (!this.drill) { + this.health -= 1 + if (this.velocity.x < 1 && this.velocity.x > -1) { + if (this.velocity.x >= 0) { + this.velocity.x += 20 + } else { + this.velocity.x -= 20 + } } else { - this.velocity.x -= 20 + this.velocity.x *= -3 } + this.velocity.y *= -2 } else { - this.velocity.x *= -3 + this.g = 1 + this.velocity.y *= -3 } - this.velocity.y *= -2 } } @@ -238,7 +248,6 @@ let player = { this.origin.y = (blockL.origin.y + level.origin.y) - this.size.y this.velocity.y = Math.min(0, this.velocity.y) this.isGrounded = true - this.g = 100 } else { level.rows[checkRow][Math.floor((left - level.origin.x) / cell.x)] = null drilled = true @@ -249,7 +258,6 @@ let player = { this.origin.y = (blockR.origin.y + level.origin.y) - this.size.y this.velocity.y = Math.min(0, this.velocity.y) this.isGrounded = true - this.g = 100 } else { level.rows[checkRow][Math.floor((right - level.origin.x) / cell.x)] = null drilled = true @@ -300,10 +308,13 @@ let player = { y: e.origin.y + e.size.y}), nextPos)) { this.isHit = true + if (this.drill) { + e.health -= 1 + } } e = e.next } - + // console.log(this.velocity.y) //move player this.origin.x += this.velocity.x this.origin.y += this.velocity.y