Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/textures/gates/No Input Buffer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 14 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@ import canvasManager from './objects/canvasManager.js';
*/
window.setup = function()
{
canvasManager.createCanvas();
sceneManager.setCurrentScene(MAIN_MENU);
let promises = [];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be a lot of noise in this PR, is this the heart of the changes you're making here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look into the noise.

for (let i of sceneManager
.getScenes()
.sceneIter())
{
promises = [...promises, ...i.preload()];
}
Promise
.all(promises)
.then(() => {
canvasManager.createCanvas();
sceneManager.setCurrentScene(MAIN_MENU);
});
}

/**
Expand All @@ -25,4 +36,4 @@ window.setup = function()
window.draw = function()
{
sceneManager.getCurrentScene().draw();
}
}
14 changes: 14 additions & 0 deletions src/objects/andGate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import TwoInputGate from "./twoInputGate.js";

export default class AndGate extends TwoInputGate
{
constructor(input1, input2, img)
{
super(input1, input2, img);
}

answer()
{
return this.input1.answer() & this.input2.answer();
}
}
15 changes: 15 additions & 0 deletions src/objects/constantGate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Gate from "./gate.js";

export default class ConstantGate extends Gate
{
constructor(value, img)
{
super(img);
this.value = value;
}

answer()
{
return this.value;
}
}
36 changes: 36 additions & 0 deletions src/objects/gate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { scaleNearestNeighbor } from "../helperFunctions/imageFunctions.js";

export default class Gate
{
constructor(img)
{
if (this.constructor === Gate)
{
throw new TypeError("Abstract class \"Gate\" cannot be instantiated directly.");
}
this.img = img;
}

answer()
{
if (this.schema === undefined)
{
throw new TypeError("Classes extending \"Gate\" must implement \"answer()\"");
}
}

show(x, y)
{
if (this.answer())
tint("#ffff00");
image(this.img, x, y);
noTint();
}

// This function should be used only occasionally
// Scaling apparently is very intensive on
showScaled(x, y, width, height)
{
image(scaleNearestNeighbor(this.img, width, height), x, y);
}
}
14 changes: 14 additions & 0 deletions src/objects/nandGate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AndGate } from "andGate.js";

export class NandGate extends AndGate
{
constructor(input1, input2, img)
{
super(input1, input2, img);
}

answer()
{
return !super.answer();
}
}
14 changes: 14 additions & 0 deletions src/objects/norGate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { OrGate } from "orGate.js";

export class NorGate extends OrGate
{
constructor(input1, input2, img)
{
super(input1, input2, img);
}

answer()
{
return !super.answer();
}
}
14 changes: 14 additions & 0 deletions src/objects/notGate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { OneInputGate } from "OneInputGate.js";

export class NotGate extends OneInputGate
{
constructor(input, img)
{
super(input, img);
}

answer()
{
return !this.input1.answer();
}
}
14 changes: 14 additions & 0 deletions src/objects/oneInputGate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Gate from "./gate.js";

export default class OneInputGate extends Gate
{
constructor(input, img)
{
super(img);
if (this.constructor === OneInputGate)
{
throw new TypeError("Abstract class \"OneInputGate\" cannot be instantiated directly.");
}
this.input1 = input;
}
}
14 changes: 14 additions & 0 deletions src/objects/orGate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { TwoInputGate } from "twoInputGate.js";

export class OrGate extends TwoInputGate
{
constructor(input1, input2, img)
{
super(input1, input2, img);
}

answer()
{
return this.input1.answer() | this.input2.answer();
}
}
5 changes: 4 additions & 1 deletion src/objects/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
* we never want to instantiate this class directly.
*/
export default class Scene {
preload() {
return [];
}
setup() {}
draw() {
background(0, 0, 0);
}
}
}
21 changes: 19 additions & 2 deletions src/objects/sceneManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ class SceneManager {
[GAME_OVER]: new GameOver(),
[INSTRUCTIONS]: new Instructions(),
[PRACTICE_MODE]: new PracticeMode(),
[SETTINGS]: new Settings()
[SETTINGS]: new Settings(),
sceneIter: function* ()
{
for (let i of sceneManager.getValidScenes())
{
yield this[i];
}
}
};

#valid_scenes = [
Expand Down Expand Up @@ -60,9 +67,19 @@ class SceneManager {
this.#current_scene = sceneName;
this.getCurrentScene().setup();
}

getScenes()
{
return this.#scenes;
}

getValidScenes()
{
return this.#valid_scenes;
}
}

// We only ever want one instance of this class (a singleton), so we will instantiate it here and then export that single instantiation.
const sceneManager = new SceneManager();

export default sceneManager;
export default sceneManager;
16 changes: 16 additions & 0 deletions src/objects/twoInputGate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import OneInputGate from "./oneInputGate.js";

export default class TwoInputGate extends OneInputGate
{
input2;

constructor(input1, input2, img)
{
super(input1, img);
if (this.constructor === TwoInputGate)
{
throw new TypeError("Abstract class \"TwoInputGate\" cannot be instantiated directly.");
}
this.input2 = input2;
}
}
14 changes: 14 additions & 0 deletions src/objects/xnorGate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { XorGate } from "xorGate.js";

export class XnorGate extends XorGate
{
constructor(input1, input2, img)
{
super(input1, input2, img);
}

answer()
{
return !super.answer();
}
}
14 changes: 14 additions & 0 deletions src/objects/xorGate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { TwoInputGate } from "twoInputGate.js";

export class XorGate extends TwoInputGate
{
constructor(input1, input2, img)
{
super(input1, input2, img);
}

answer()
{
return this.input1.answer() ^ this.input2.answer();
}
}
64 changes: 62 additions & 2 deletions src/scenes/challengeMode.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,75 @@
import Scene from "../objects/scene.js";
import ConstantGate from "../objects/constantGate.js";
import AndGate from "../objects/andGate.js";

/**
* This scene will manage the game's challenge mode.
*/
export default class ChallengeMode extends Scene {
constructor() {
super();
this.bufferImg = null;
this.notImg = null;
this.andImg = null;
this.orImg = null;
this.xorImg = null;
this.nandImg = null;
this.norImg = null;
this.xnorImg = null;
this.constant1 = null;
this.constant2 = null;
this.gate = null;
}

setup() {}
preload()
{
let promises = [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this be made redundant by GateFactory.preload() in #51 ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it will, it is basically all thrown into the gate factory preloader, that mess is for demonstration purposes.

new Promise((resolve, reject) => {
this.bufferImg = loadImage("assets/textures/gates/No Input Buffer.png");
resolve(true);
}),
new Promise((resolve, reject) => {
this.notImg = loadImage("assets/textures/gates/NOT Gate.png");
resolve(true);
}),
new Promise((resolve, reject) => {
this.andImg = loadImage("assets/textures/gates/AND Gate.png");
resolve(true);
}),
new Promise((resolve, reject) => {
this.orImg = loadImage("assets/textures/gates/OR Gate.png");
resolve(true);
}),
new Promise((resolve, reject) => {
this.xorImg = loadImage("assets/textures/gates/XOR Gate.png");
resolve(true);
}),
new Promise((resolve, reject) => {
this.nandImg = loadImage("assets/textures/gates/NAND Gate.png");
resolve(true);
}),
new Promise((resolve, reject) => {
this.norImg = loadImage("assets/textures/gates/NOR Gate.png");
resolve(true);
}),
new Promise((resolve, reject) => {
this.xnorImg = loadImage("assets/textures/gates/XNOR Gate.png");
resolve(true);
})
];
return promises;
}

setup() {
this.constant1 = new ConstantGate(1, this.bufferImg);
this.constant2 = new ConstantGate(1, this.bufferImg);
this.gate = new AndGate(this.constant1, this.constant2, this.andImg);
}

draw() {
background(0, 0, 255);
this.constant1.show(100, 100);
this.constant2.show(100, 200);
this.gate.show(150, 150);
}
}
}
Loading