-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for multiple backgrounds
- Loading branch information
Showing
9 changed files
with
277 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
'use strict'; | ||
|
||
import * as Blockly from 'blockly/core'; | ||
|
||
import {useBackgroundsStorage} from '../hooks/project'; | ||
import {playfieldToMatrix} from '../utils/pixels'; | ||
import {BACKGROUND_ICON} from './icon'; | ||
|
||
const BACKGROUND_COLOR = '#ffa500'; | ||
|
||
const backgroundsStorage = useBackgroundsStorage(); | ||
|
||
const buildBackgroundOptions = () => { | ||
try { | ||
// TODO: Share this constant with BackgroundEditor.vue | ||
const defaultBackgrounds = { | ||
backgrounds: [ | ||
{ | ||
id: 1, | ||
name: 'Test 1', | ||
pixels: playfieldToMatrix( | ||
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n' + | ||
'X....X...................X....X\n' + | ||
'X.............................X\n' + | ||
'X.............................X\n' + | ||
'X.............................X\n' + | ||
'X.............................X\n' + | ||
'X.............................X\n' + | ||
'X.............................X\n' + | ||
'X.............................X\n' + | ||
'X....X...................X....X\n' + | ||
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'), | ||
}, | ||
], | ||
}; | ||
|
||
const background = backgroundsStorage.value || defaultBackgrounds; | ||
|
||
return background.backgrounds.map(({id, name}) => [name || `Unnamed ${id}`, `${id}`]); | ||
} catch (e) { | ||
console.error('Failed to list background options', e); | ||
return [[1, 'Error']]; | ||
} | ||
}; | ||
|
||
Blockly.defineBlocksWithJsonArray([ | ||
// Block for selecting a background. | ||
{ | ||
'type': `background_select`, | ||
'message0': `${BACKGROUND_ICON} Background: %1`, | ||
'args0': [ | ||
{ | ||
'type': 'field_dropdown', | ||
'name': 'VAR', | ||
'options': buildBackgroundOptions(), | ||
}, | ||
], | ||
'output': 'Number', | ||
'colour': BACKGROUND_COLOR, | ||
'tooltip': `Selects a background`, | ||
}, | ||
// Block for the setter. | ||
{ | ||
'type': `background_set`, | ||
'message0': `${BACKGROUND_ICON} Background: set to: %1`, | ||
'args0': [ | ||
{ | ||
'type': 'input_value', | ||
'name': 'VALUE', | ||
}, | ||
], | ||
'previousStatement': null, | ||
'nextStatement': null, | ||
'colour': BACKGROUND_COLOR, | ||
'tooltip': `Updates the background`, | ||
}, | ||
// Block for the setter with an internal select | ||
{ | ||
'type': `background_set_select`, | ||
'message0': `${BACKGROUND_ICON} Background: %1`, | ||
'args0': [ | ||
{ | ||
'type': 'field_dropdown', | ||
'name': 'VAR', | ||
'options': buildBackgroundOptions(), | ||
}, | ||
], | ||
'previousStatement': null, | ||
'nextStatement': null, | ||
'colour': BACKGROUND_COLOR, | ||
'tooltip': `Updates the background`, | ||
}, | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
export default (Blockly) => { | ||
Blockly.BBasic[`background_select`] = function(block) { | ||
const code = block.getFieldValue('VAR') || 0; | ||
return [code, Blockly.BBasic.ORDER_ATOMIC]; | ||
}; | ||
|
||
Blockly.BBasic[`background_set`] = function(block) { | ||
// Score setter. | ||
const argument0 = Blockly.BBasic.valueToCode(block, 'VALUE', | ||
Blockly.BBasic.ORDER_ASSIGNMENT) || '0'; | ||
return 'newbackground = ' + argument0 + '\n'; | ||
}; | ||
Blockly.BBasic[`background_set_select`] = function(block) { | ||
// Score setter. | ||
const argument0 = block.getFieldValue('VAR') || 0; | ||
return 'newbackground = ' + argument0 + '\n'; | ||
}; | ||
}; | ||
|
Oops, something went wrong.