Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gregv21v committed Feb 23, 2024
1 parent 8d1a870 commit b6a596f
Show file tree
Hide file tree
Showing 20 changed files with 388 additions and 212 deletions.
7 changes: 7 additions & 0 deletions GameState.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const Direction = {
Left: 0,
Right: 1,
Up: 2,
Down: 3,
Stop: 4
}
17 changes: 9 additions & 8 deletions Grid.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

class Grid {

/**
Expand Down Expand Up @@ -60,15 +61,15 @@ class Grid {
getNextSlot(pointer, direction) {
// choose the direction the emitter goes
// and update the pointer accordingly.
if(direction === "right") {
return {x: pointer.x + 1 , y: pointer.y}
} else if(direction === "left") {
if(direction === Direction.Right) {
return {x: pointer.x + 1, y: pointer.y}
} else if(direction === Direction.Left) {
return {x: pointer.x - 1, y: pointer.y}
} else if(direction === "down") {
} else if(direction === Direction.Down) {
return {x: pointer.x, y: pointer.y + 1}
} else if(direction === "up") {
} else if(direction === Direction.Up) {
return {x: pointer.x, y: pointer.y - 1}
} else if(direction === "stop") {
} else if(direction === Direction.Stop) {
return {x: pointer.x, y: pointer.y}
}
}
Expand All @@ -83,7 +84,7 @@ class Grid {
*/
getNextDirection(direction, slot) {
if(slot.item) {
return slot.item.updateDirection(direction);
return slot.item.getNextDirections(direction);
} else {
return direction;
}
Expand Down Expand Up @@ -160,7 +161,7 @@ class Grid {
if(slot && slot.item && !(slot.item instanceof Emitter)) {
//slot.item.inDirection = startNode.direction; // set the in direction

let children = slot.item.updateNode(this, startNode);
let children = slot.item.getNextNode(this, startNode);


if(children) {
Expand Down
23 changes: 3 additions & 20 deletions InfiniteGrid.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

class InfiniteGrid extends Grid {

/**
Expand Down Expand Up @@ -33,25 +34,7 @@ class InfiniteGrid extends Grid {



/**
* getNextSlot()
* @param {Point} pointer the pointer to the current position of the laser
* @param {Direction} direction the direction the laser is going
*/
getNextSlot(pointer, direction) {

// choose the direction the emitter goes
// and update the pointer accordingly.
if(direction === "right") {
return {x: pointer.x + 1 , y: pointer.y}
} else if(direction === "left") {
return {x: pointer.x - 1, y: pointer.y}
} else if(direction === "down") {
return {x: pointer.x, y: pointer.y + 1}
} else if(direction === "up") {
return {x: pointer.x, y: pointer.y - 1}
}
}



/**
Expand All @@ -63,7 +46,7 @@ class InfiniteGrid extends Grid {
*/
getNextDirection(direction, slot) {
if(slot.item) {
return slot.item.updateDirection(direction);
return slot.item.getNextDirection(direction);
} else {
return direction;
}
Expand Down
127 changes: 71 additions & 56 deletions gameObjects/BeamExpander.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,22 @@

/**
* BeamExpander - expands the laser beam when it enters this GameObject.
*/
class BeamExpander extends GameObject {

/**
constructor()
@description constructs the block
*/
* constructor()
* @description constructs this BeamExpander
* @param {Point} position the position of this GameObject
*/
constructor(position = {x: 0, y: 0}) {
super(position);
this._rotation = 0;
}

/**
* clone()
* @description clones the BeamExpander
* @returns {BeamExpander} a clone of the BeamExpander
*/
clone() {
let newBeamExpander = new BeamExpander();
newBeamExpander._position = this._position;

return newBeamExpander;
}


updateNode(grid, node) {
node.width += 1;
let nextPoint = grid.getNextSlot(node.point, node.direction);
let nextDirection = this.updateDirection(node.direction);
if(grid.pointInGrid(nextPoint) && nextDirection !== "stop") {
return {
...node,
direction: this.updateDirection(node.direction),
point: nextPoint,
width: node.width,
children: []
}
} else {
return null;
}

}

/**
* updateDirection()
* @description updates the direction of the laser
* @param {direction} direction the current direction of the laser
* @returns the new direction of the laser
* _createPath()
* @description creates the path of the graphic for the GameObject
*/
updateDirection(direction) {

while(this._rotation < 0) this._rotation += 360;

const directionMap = {
0: { right: "right"},
90: { down: "down"},
180: { left: "left"},
270: { up: "up"}
};

return directionMap[(this._rotation % 360)][direction] || "stop";
}

_createPath() {
this._path = [];

Expand Down Expand Up @@ -98,9 +54,10 @@ class BeamExpander extends GameObject {
}

/**
render()
@description initialize the values for the svg
*/
* render()
* @description renders the beam expander graphic
* @param {CanvasRenderingContext2D} context the context to render to
*/
render(context) {
this._createPath();
this._applyRotation(this._rotation);
Expand All @@ -119,4 +76,62 @@ class BeamExpander extends GameObject {
}




/**
* getNextNode()
* @description gets the next node in the lasers path
* @param {Grid} grid the grid this node is part of
* @param {Node} node the laser node
* @returns the next node in the laser path
*/
getNextNode(grid, node) {
node.width += 1;
let nextPoint = grid.getNextSlot(node.point, node.direction);
let nextDirection = this.getNextDirections(node.direction)[0];
if(grid.pointInGrid(nextPoint) && nextDirection !== Direction.Stop) {
return {
...node,
direction: nextDirection,
point: nextPoint,
width: node.width,
children: []
}
} else {
return null;
}

}

/**
* getNextDirection()
* @description gets the direction that comes after this one in the laser path.
* @param {direction} direction the current direction of the laser
* @returns the new direction of the laser
*/
getNextDirections(direction) {

while(this._rotation < 0) this._rotation += 360;

const directionMap = {
0: { [Direction.Right]: Direction.Right },
90: { [Direction.Down]: Direction.Down },
180: { [Direction.Left]: Direction.Left },
270: { [Direction.Up]: Direction.Up }
};

return [directionMap[(this._rotation % 360)][direction] || Direction.Stop];
}

/**
* clone()
* @description clones the BeamExpander
* @returns {BeamExpander} a clone of the BeamExpander
*/
clone() {
let newBeamExpander = new BeamExpander(this._position);
return newBeamExpander;
}


}
72 changes: 47 additions & 25 deletions gameObjects/BeamSplitter.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@

class BeamSplitter extends GameObject {
/**
constructor()
@description constructs the block
*/
* constructor()
* @description constructs this BeamSplitter
* @param {Point} position the position of this GameObject
*/
constructor(position = {x: 0, y: 0}) {
super(position);
this._rotation = 45;
this._directions = ["down", "right"]

this._rotation = 45; // the rotation of the BeamSplitter
}


updateNode(grid, node) {
return this.getDirections(node.direction).map(direction => {
/**
* getNextNode()
* @description gets the next node in the lasers path
* @param {Grid} grid the grid this node is part of
* @param {Node} node the laser node
* @returns the next node in the laser path
*/
getNextNode(grid, node) {
return this.getNextDirections(node.direction).map(direction => {
let nextPoint = grid.getNextSlot(node.point, direction)

if(grid.pointInGrid(nextPoint)) {
Expand All @@ -28,19 +35,7 @@ class BeamSplitter extends GameObject {
})
}

/**
* clone()
* @description clones the BeamSplitter
* @returns {BeamSplitter} a clone of the BeamSplitter
*/
clone() {
let newBeamSplitter = new BeamSplitter();
newBeamSplitter._position = this._position;
newBeamSplitter._direction = this._direction;
newBeamSplitter._rotation = this._rotation;

return newBeamSplitter;
}


_createPath() {
this._path = [];
Expand Down Expand Up @@ -107,12 +102,39 @@ class BeamSplitter extends GameObject {

}

getDirections(inputDirection) {
getNextDirections(inputDirection) {
while(this._rotation < 0) this._rotation += 360;

let map = {
0: { down: "right", right: "down", up: "left", left: "up"},
90: { down: "left", right: "up", up: "right", left: "down"}
0: {
[Direction.Down]: Direction.Right,
[Direction.Right]: Direction.Down,
[Direction.Up]: Direction.Left,
[Direction.Left]: Direction.Up
},
90: {
[Direction.Down]: Direction.Left,
[Direction.Right]: Direction.Up,
[Direction.Up]: Direction.Right,
[Direction.Left]: Direction.Down
}
}
return [map[this._rotation - 45][inputDirection]].concat(inputDirection)
return [map[(this._rotation - 45) % 180][inputDirection]].concat(inputDirection)
}


/**
* clone()
* @description clones the BeamSplitter
* @returns {BeamSplitter} a clone of the BeamSplitter
*/
clone() {
let newBeamSplitter = new BeamSplitter();
newBeamSplitter._position = this._position;
newBeamSplitter._direction = this._direction;
newBeamSplitter._rotation = this._rotation;

return newBeamSplitter;
}


Expand Down
7 changes: 7 additions & 0 deletions gameObjects/Bit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Toggle - when a pulse hits this component, a value of one is stored. When another pulse hits this component
* a value of zero is stored
*/
class Toggle extends Component {


18 changes: 13 additions & 5 deletions gameObjects/Block.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/**
* Block - blocks the laser beam travel
*/
Expand Down Expand Up @@ -33,18 +34,25 @@ class Block extends GameObject {
}


updateNode(grid, node) {
/**
* getNextNode()
* @description gets the next node in the lasers path
* @param {Grid} grid the grid this node is part of
* @param {Node} node the laser node
* @returns the next node in the laser path
*/
getNextNode(grid, node) {
this._needsUpdate = false;
return null;
}


updateDirection(direction) {
return "stop";
getNextDirections(direction) {
return [Direction.Stop];
}

reverseDirection(direction) {
return
getPreviousDirections(direction) {
return [];
}
}

5 changes: 5 additions & 0 deletions gameObjects/CounterTarget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* CounterTarget - a target that will wait for some number of pulses before it is activated.
*
*
*/
Loading

0 comments on commit b6a596f

Please sign in to comment.