Skip to content

Commit

Permalink
Simplify variables API
Browse files Browse the repository at this point in the history
  • Loading branch information
PullJosh committed Nov 16, 2019
1 parent d5179c1 commit dc27d61
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 91 deletions.
4 changes: 3 additions & 1 deletion example-project/Cat/Cat.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ export default class Cat extends Sprite {

* turn() {
for(let i = 0; i < 36; i++) {
console.log(this.stage.vars.myGlobalVar)
yield* this.broadcastAndWait('turn dog')
this.direction += 10
this.direction += this.vars.speed
this.vars.speed += 1
}
}
}
1 change: 1 addition & 0 deletions example-project/Dog/Dog.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default class Dog extends Sprite {
* turn() {
for(let i = 0; i < 36; i++) {
this.direction += 10
this.stage.vars.myGlobalVar = this.random(0, 100)
yield
}
}
Expand Down
28 changes: 16 additions & 12 deletions example-project/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import Stage from './Stage/Stage.mjs'
import Dog from './Dog/Dog.mjs'
import Cat from './Cat/Cat.mjs'

const stage = new Stage({
costumeNumber: 1
})
const stage = new Stage(
{ costumeNumber: 1 },
{ myGlobalVar: "hello" }
)

const sprites = [
new Cat({
x: -100,
y: 0,
direction: 90,
costumeNumber: 1,
size: 100,
visible: true,
penDown: true
}),
new Cat(
{
x: -100,
y: 0,
direction: 90,
costumeNumber: 1,
size: 100,
visible: true,
penDown: true
},
{ speed: 1 }
),
new Dog({
x: 100,
y: 0,
Expand Down
5 changes: 1 addition & 4 deletions scratch-js/Project.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import Trigger from './Trigger.mjs'
import Renderer from './Renderer.mjs'
import Input from './Input.mjs'
import Vars from './Vars.mjs'

export default class Project {
constructor(stage, sprites = [], globalVars = new Vars()) {
constructor(stage, sprites = []) {
this.stage = stage
this.sprites = sprites

Expand All @@ -13,8 +12,6 @@ export default class Project {
}
this.stage._project = this

this._vars = globalVars

this.renderer = new Renderer('#project')
this.input = new Input(this.renderer.stage, key => {
this.fireTrigger(Trigger.KEY_PRESSED, { key })
Expand Down
23 changes: 16 additions & 7 deletions scratch-js/Sprite.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import Trigger from './Trigger.mjs'

class SpriteBase {
constructor(initialConditions) {
constructor(initialConditions, vars = {}) {
this._project = null

const { costumeNumber } = initialConditions

this.costumeNumber = costumeNumber

this.triggers = []
this.costumes = []

this._vars = vars
}

get vars() {
return this._vars
}

set costume(costume) {
Expand Down Expand Up @@ -104,8 +111,8 @@ class SpriteBase {
}

export class Sprite extends SpriteBase {
constructor(initialConditions, vars = {}) {
super(initialConditions)
constructor(initialConditions, ...args) {
super(initialConditions, ...args)

const { x, y, direction, costumeNumber, size, visible, penDown, penSize, penColor } = initialConditions

Expand All @@ -119,8 +126,10 @@ export class Sprite extends SpriteBase {
this._penDown = penDown || false
this.penSize = penSize || 1
this.penColor = penColor || 'blue'
}

this._vars = vars
get stage() {
return this._project.stage
}

get direction() {
Expand Down Expand Up @@ -235,7 +244,7 @@ export class Sprite extends SpriteBase {
}

export class Stage extends SpriteBase {
constructor(initialConditions) {
super(initialConditions)
constructor(...args) {
super(...args)
}
}
4 changes: 2 additions & 2 deletions scratch-js/Trigger.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export default class Trigger {
return true
}

start(...args) {
start() {
this.stop()

this.done = false
this._scriptRunning = this._script(...args)
this._scriptRunning = this._script()

return new Promise(resolve => {
this.stop = () => {
Expand Down
63 changes: 0 additions & 63 deletions scratch-js/Vars.mjs

This file was deleted.

3 changes: 1 addition & 2 deletions scratch-js/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ import Project from './Project.mjs'
import { Sprite, Stage } from './Sprite.mjs'
import Trigger from './Trigger.mjs'
import Costume from './Costume.mjs'
import Vars from './Vars.mjs'

export { Project, Sprite, Stage, Trigger, Costume, Vars }
export { Project, Sprite, Stage, Trigger, Costume }

0 comments on commit dc27d61

Please sign in to comment.