diff options
author | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2024-12-26 14:24:53 +0100 |
---|---|---|
committer | niliara-edu <nil.jimeno@estudiant.fjaverianas.com> | 2024-12-26 14:24:53 +0100 |
commit | 474a7253b6b67e2ed33936f6b633587d5d304b66 (patch) | |
tree | 456b9cef3cb59da8e0eb575b9425a1fedfe67e1e /src/engine | |
parent | 1fc522bc8c4a96858223e597ced8fea94ba57874 (diff) |
hello kitty stage 1 and 2 done
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/screen.js | 51 | ||||
-rw-r--r-- | src/engine/vector.js | 36 |
2 files changed, 79 insertions, 8 deletions
diff --git a/src/engine/screen.js b/src/engine/screen.js index e9e5ef4..b1da92d 100644 --- a/src/engine/screen.js +++ b/src/engine/screen.js @@ -76,11 +76,12 @@ export class Screen { ///////////////////// COMMANDS ////////////////////// append(element) { this.spawn.appendChild(element) } - draw(span, position, size, /* rotation = 0 */) { + draw(span, position, size, rotation) { span.style.width = `${size.x * this.scale}px` span.style.height = `${size.y * this.scale}px` - span.style.top = `${(position.y - size.y / 2) * this.scale}px` - span.style.left = `${(position.x - size.x / 2) * this.scale}px` + span.style.top = `${(position.y - size.y / 2 + this.additionalPosition.y) * this.scale}px` + span.style.left = `${(position.x - size.x / 2 + this.additionalPosition.x) * this.scale}px` + span.style.rotate = `${rotation}deg` } setBackground(img) { this.background.src = img } @@ -95,4 +96,48 @@ export class Screen { position.y - size.y / 2 > this.viewport.y ) } + + shaking = false + additionalPosition = new Vector(0, 0) + shake() { + this.shaking = true + this.shakingTimeLeft = 6 + } + + update() { + if (this.shaking) { + switch (this.shakingTimeLeft) { + case 6: + this.additionalPosition.x = 9 + this.additionalPosition.y = -3 + break + + case 5: + this.additionalPosition.x = -8 + this.additionalPosition.y = 2 + break + + case 4: + this.additionalPosition.x = 5 + this.additionalPosition.y = 1 + break + + case 3: + this.additionalPosition.x = -4 + this.additionalPosition.y = -2 + break + + case 2: + this.additionalPosition.x = 3 + this.additionalPosition.y = -1 + break + + case 1: + this.additionalPosition.x = 0 + this.shaking = false + break + } + this.shakingTimeLeft -- + } + } } diff --git a/src/engine/vector.js b/src/engine/vector.js index f596c51..739daaf 100644 --- a/src/engine/vector.js +++ b/src/engine/vector.js @@ -11,12 +11,17 @@ export class Vector { normalize() { if (this.x != 0 && 0 != this.y) { - let hyp = Math.abs(this.x) - let cSquared = Math.pow(hyp, 2) / 2 - let c = Math.sqrt(cSquared, 2) + /* this is what it should be doing: */ + // let hyp = Math.abs(this.x) + // let cSquared = Math.pow(hyp, 2) / 2 + // let c = Math.sqrt(cSquared, 2) - this.x = this.x / Math.abs(this.x) * c - this.y = this.y / Math.abs(this.x) * c + // this.x = this.x / Math.abs(this.x) * c + // this.y = this.y / Math.abs(this.x) * c + + /* but we need to save resources */ + this.x = this.x / 4 * 3 + this.y = this.y / 4 * 3 } } @@ -29,6 +34,13 @@ export class Vector { return new Vector(this.x, this.y) } + static substraction(vector_a, vector_b) { + return new Vector( + vector_a.x - vector_b.x, + vector_a.y - vector_b.y + ) + } + static division(vector_a, vector_b) { return new Vector( vector_a.x / vector_b.x, @@ -41,4 +53,18 @@ export class Vector { this.y = Math.floor(this.y) return this } + + toDeg() { + var angle = Math.atan2(this.y, this.x) //radians + var degrees = 180*angle/Math.PI //degrees + return (360+Math.round(degrees))%360 //round number, avoid decimal fragments + } + + static fromDeg(degrees) { + let radians = degrees*Math.PI/180 + return new Vector( + Math.cos(radians), + Math.sin(radians) + ) + } } |