-
Notifications
You must be signed in to change notification settings - Fork 0
Added preloader to avoid network calls and computationally expensive scaling between scenes #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| } | ||
| } |
| 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; | ||
| } | ||
| } |
| 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); | ||
| } | ||
| } |
| 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(); | ||
| } | ||
| } |
| 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(); | ||
| } | ||
| } |
| 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(); | ||
| } | ||
| } |
| 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; | ||
| } | ||
| } |
| 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(); | ||
| } | ||
| } |
| 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; | ||
| } | ||
| } |
| 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(); | ||
| } | ||
| } |
| 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(); | ||
| } | ||
| } |
| 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 = [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this be made redundant by
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.