Skip to content

Commit

Permalink
A bit of a refactor
Browse files Browse the repository at this point in the history
* Pulled out scoreboard
* Pulled ou overlay
* Reworked game instantiation
  • Loading branch information
justino committed Nov 15, 2022
1 parent c01f97d commit bfdfb94
Show file tree
Hide file tree
Showing 23 changed files with 380 additions and 381 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ DEREZ EVERYTHING THAT MOVES

Each enemy type in the game is worth a certain amount of points.
Killing them will earn you points.
Get hit though, and you'll lose points. Go negative and you'll have to dig yourself out of your hole.
Get hit though, and you'll lose points.

## Enemies

Expand Down
4 changes: 2 additions & 2 deletions css/tran.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ main {
align-items: stretch;
}

.overlay {
#overlay {
z-index: 1;
display: none;
justify-content: center;
Expand All @@ -68,7 +68,7 @@ main {
bottom: 0;
flex: 1;
}
.overlay.show {
#overlay.show {
display: flex;
}

Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<main>
<div class="gamegrid">
<canvas id="gamegrid"></canvas>
<div class="overlay">
<div id="overlay">
<button class="start">Enter the Game Grid</button>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion js/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ export default {
homingDiscColor: 'rgba(255, 255, 255, 1)'

// Debuging Params
};
}
6 changes: 3 additions & 3 deletions js/discs/beginner.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Disc } from "./disc.js";
import { Disc } from "./disc.js"

export class BeginnerDisc extends Disc {
constructor(unit) {
super('Beginner', unit.gameGrid.config.beginnerDiscColor, unit);
super('Beginner', config.beginnerDiscColor, unit)

this.baseSpeed = 4;
this.baseSpeed = 4
}
}
111 changes: 52 additions & 59 deletions js/discs/disc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,48 @@ export class Disc extends Sprite {
RETURNING = Symbol("returning")

constructor(name, color, unit) {
super(unit.gameGrid, name, unit.gameGrid.config.discSize, unit.gameGrid.config.discSize, color, unit.location)
super(unit.gameGrid, name, config.discSize, config.discSize, color, unit.location)

this.owner = unit;
this.owner = unit

this.strength = 1;
this.status = this.HELD; // deadly, bouncing, returning
this.speedModifier = 0;
this.velocity = null;
this.strength = 1
this.status = this.HELD // deadly, bouncing, returning
this.speedModifier = 0
this.velocity = null

this.returnable = false;
this.primed = false;
this.collided = null;
this.returnable = false
this.primed = false
this.collided = null
}

Update() {
switch (this.status) {
case this.HELD:
// Follow owner around
this.location = Vector.Clone(this.owner.location);
break;
this.location = Vector.Clone(this.owner.location)
break
case this.RETURNING:
this.Return();
break;
this.Return()
break
case this.DEADLY:
case this.BOUNCING:
// Basic Straight Lines
this.location.Add(this.velocity);
break;
this.location.Add(this.velocity)
break
}

this.checkBounce();
this.checkBounce()
}

checkBounce() {
const bounced = this.bindToGameGrid();
const bounced = this.bindToGameGrid()
if (bounced[0] || bounced[1]) {
if (bounced[0]) { this.BounceX(); }
if (bounced[1]) { this.BounceY(); }
if (bounced[0]) this.BounceX()
if (bounced[1]) this.BounceY()

if (this.status !== this.BOUNCING) {
this.status = this.BOUNCING;
setTimeout(this.Return.bind(this), this.owner.gameGrid.config.discReturnTime);
this.status = this.BOUNCING
setTimeout(this.Return.bind(this), config.discReturnTime)
}
}
}
Expand All @@ -60,92 +60,85 @@ export class Disc extends Sprite {
case this.BOUNCING:
case this.RETURNING:
// Square
if (this.height !== this.owner.gameGrid.config.discSize) { this.changeHeight(this.owner.gameGrid.config.discSize); }
this.DrawSprite();
break;
if (this.height !== config.discSize) this.changeHeight(config.discSize)
this.DrawSprite()
break
case this.DEADLY:
// Flat
if (this.height !== this.owner.gameGrid.config.discSize / 2) { this.changeHeight(this.owner.gameGrid.config.discSize / 2); }
this.DrawSprite();
break;
if (this.height !== config.discSize / 2) this.changeHeight(config.discSize / 2)
this.DrawSprite()
break
}
}

checkCollide(unit) {
// We don't care about non-deadly discs
if (this.status !== this.DEADLY)
return;
if (this.status !== this.DEADLY) return

const collision = this.Collision(unit);
const collision = this.Collision(unit)

/* If the disc has already collided with the current unit
ignore, we don't want to hit them again until we've stopped colliding
with them */
if (unit === this.collided && collision) {
return;
}
if (unit === this.collided && collision) return

/* If the disc is marked as being collided with this unit
but it isn't collided any more, unmark it. */
if (unit === this.collided && !collision) {
this.collided = null;
}
if (unit === this.collided && !collision) this.collided = null

/* If this disc has collided with this unit
then mark the disc as being collided with this unit */
if (collision) {
this.collided = unit;
}
if (collision) this.collided = unit

if (collision) {
dispatchEvent(new CustomEvent('UnitHit', {
detail: {
winner: this.owner,
loser: unit
}
}));
}))

// When a collision occurs, attempt to regenerate the disc owner
// if they are capable
this.owner.Regenerate();
this.owner.Regenerate()
// Bounce off unit if they are alive, otherwise pass through corpse
if (! unit.isDead()) this.Return();
if (! unit.isDead()) this.Return()
}
}

Thrown(direction) {
this.status = this.DEADLY;
this.status = this.DEADLY

const velocity = new Vector([0, 0]);
const velocity = new Vector([0, 0])

direction.Mul(this.baseSpeed + this.speedModifier);
direction.Mul(this.baseSpeed + this.speedModifier)

velocity.Add(direction);
velocity.Limit(this.baseSpeed + this.speedModfier);
this.velocity = velocity;
velocity.Add(direction)
velocity.Limit(this.baseSpeed + this.speedModfier)
this.velocity = velocity
}

Return() {
this.status = this.RETURNING;
this.returnable = false;
this.status = this.RETURNING
this.returnable = false

this.velocity = new Vector([0, 0]);
this.velocity = new Vector([0, 0])

const ownerForce = Vector.SubFactory(this.owner.location, this.location);
ownerForce.Normalize();
ownerForce.Mul(this.baseSpeed + this.speedModifier);
const ownerForce = Vector.SubFactory(this.owner.location, this.location)
ownerForce.Normalize()
ownerForce.Mul(this.baseSpeed + this.speedModifier)

this.velocity.Add(ownerForce);
this.velocity.Limit(this.baseSpeed + this.speedModifier);
this.velocity.Add(ownerForce)
this.velocity.Limit(this.baseSpeed + this.speedModifier)

this.location.Add(this.velocity);
this.location.Add(this.velocity)
}

BounceX() {
this.velocity.points[0] *= -1;
this.velocity.points[0] *= -1
}

BounceY() {
this.velocity.points[1] *= -1;
this.velocity.points[1] *= -1
}
}
50 changes: 25 additions & 25 deletions js/discs/homing.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
import { Vector } from "../vector.js";
import { Vector } from "../vector.js"
import { Disc } from "./disc.js"

export class HomingDisc extends Disc {
constructor(unit) {
super('Homing', unit.gameGrid.config.homingDiscColor, unit)
super('Homing', config.homingDiscColor, unit)

this.baseSpeed = 2;
this.homing = false;
this.baseSpeed = 2
this.homing = false
}

Update() {
switch (this.status) {
case this.HELD:
// Follow owner around
this.location = Vector.Clone(this.owner.location);
break;
this.location = Vector.Clone(this.owner.location)
break
case this.RETURNING:
this.Return();
break;
this.Return()
break
case this.DEADLY:
if (this.homing) {
this.homeInOnPlayer();
break;
this.homeInOnPlayer()
break
}
case this.BOUNCING:
// Basic Straight Lines
this.location.Add(this.velocity);
break;
this.location.Add(this.velocity)
break
}

this.checkBounce();
this.checkBounce()
}

Thrown(direction) {
// After one second of regular movement, start homing
setTimeout(this.startHoming.bind(this), 1000);
super.Thrown(direction);
setTimeout(this.startHoming.bind(this), 1000)
super.Thrown(direction)
}

Return() {
this.homing = false;
super.Return();
this.homing = false
super.Return()
}

startHoming() {
this.homing = true;
this.homing = true
}

homeInOnPlayer() {
this.velocity = new Vector([0, 0]);
this.velocity = new Vector([0, 0])

const playerForce = Vector.SubFactory(this.gameGrid.player.location, this.location);
playerForce.Normalize();
playerForce.Mul(this.baseSpeed + this.speedModifier);
const playerForce = Vector.SubFactory(this.gameGrid.player.location, this.location)
playerForce.Normalize()
playerForce.Mul(this.baseSpeed + this.speedModifier)

this.velocity.Add(playerForce);
this.velocity.Limit(this.baseSpeed + this.speedModifier);
this.velocity.Add(playerForce)
this.velocity.Limit(this.baseSpeed + this.speedModifier)

this.location.Add(this.velocity);
this.location.Add(this.velocity)
}
}
6 changes: 3 additions & 3 deletions js/discs/intermediate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Disc } from "./disc.js"

export class IntermediateDisc extends Disc {
constructor(unit) {
super('Intermediate', unit.gameGrid.config.intermediateDiscColor, unit);
super('Intermediate', config.intermediateDiscColor, unit)

this.baseSpeed = 4;
this.strength = 2;
this.baseSpeed = 4
this.strength = 2
}
}
4 changes: 2 additions & 2 deletions js/discs/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Disc } from "./disc.js"

export class PlayerDisc extends Disc {
constructor(unit) {
super('Player', unit.gameGrid.config.playerDiscColor, unit)
super('Player', config.playerDiscColor, unit)

this.baseSpeed = 4;
this.baseSpeed = 4
}
}
Loading

0 comments on commit bfdfb94

Please sign in to comment.