Skip to content

Commit

Permalink
Implement score
Browse files Browse the repository at this point in the history
This fixes #21 
Merge pull request #33 from haroldo-ok/implement-score
  • Loading branch information
haroldo-ok authored Jan 22, 2023
2 parents ee63c6b + c53b3f7 commit 113a247
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vcs-game-maker",
"version": "0.8.1",
"version": "0.9.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
Expand Down
1 change: 1 addition & 0 deletions src/blocks/icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export const HEIGHT_ICON = String.fromCodePoint(0x1F4CF);
export const JOYSTICK_ICON = String.fromCodePoint(0x1F579);
export const FIRE_ICON = String.fromCodePoint(0x1F518);
export const PLAYFIELD_ICON = String.fromCodePoint(0x1F5FA);
export const SCORE_ICON = String.fromCodePoint(0x1F4AF);
47 changes: 47 additions & 0 deletions src/blocks/score.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as Blockly from 'blockly/core';

import {SCORE_ICON} from './icon';

const SCORE_COLOR = '#f2691e';

Blockly.defineBlocksWithJsonArray([
// Block for the getter.
{
'type': `score_get`,
'message0': `${SCORE_ICON} Score`,
'output': 'Number',
'colour': SCORE_COLOR,
'tooltip': `Reads the score`,
},
// Block for the setter.
{
'type': `score_set`,
'message0': `${SCORE_ICON} Score: set to: %1`,
'args0': [
{
'type': 'input_value',
'name': 'VALUE',
},
],
'previousStatement': null,
'nextStatement': null,
'colour': SCORE_COLOR,
'tooltip': `Updates the score`,
},
// Block for adding to a variable in place.
{
'type': `score_change`,
'message0': `${SCORE_ICON} Score: change by: %1`,
'args0': [
{
'type': 'input_value',
'name': 'DELTA',
'check': 'Number',
},
],
'previousStatement': null,
'nextStatement': null,
'colour': SCORE_COLOR,
'extensions': ['math_change_tooltip'],
},
]);
1 change: 1 addition & 0 deletions src/components/ActionEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import BlocklyComponent from './BlocklyComponent.vue';
import '../blocks/collision';
import '../blocks/input';
import '../blocks/sprites';
import '../blocks/score';
import blocklyToolbox from 'raw-loader!./blockly-toolbox.xml';
import BlocklyBB from '../generators/bbasic';
import {useWorkspaceStorage} from '../hooks/project';
Expand Down
57 changes: 52 additions & 5 deletions src/components/blockly-toolbox.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
<category name="Sprites" colour="red">

<block type="sprite_player0_get"></block>
<block type="sprite_player0_set"></block>
<block type="sprite_player0_set">
<value name="VALUE">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
<block type="sprite_player0_change">
<value name="DELTA">
<shadow type="math_number">
Expand All @@ -38,7 +44,13 @@
</block>

<block type="sprite_player1_get"></block>
<block type="sprite_player1_set"></block>
<block type="sprite_player1_set">
<value name="VALUE">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
<block type="sprite_player1_change">
<value name="DELTA">
<shadow type="math_number">
Expand All @@ -48,7 +60,13 @@
</block>

<block type="sprite_missile0_get"></block>
<block type="sprite_missile0_set"></block>
<block type="sprite_missile0_set">
<value name="VALUE">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
<block type="sprite_missile0_change">
<value name="DELTA">
<shadow type="math_number">
Expand All @@ -58,7 +76,13 @@
</block>

<block type="sprite_missile1_get"></block>
<block type="sprite_missile1_set"></block>
<block type="sprite_missile1_set">
<value name="VALUE">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
<block type="sprite_missile1_change">
<value name="DELTA">
<shadow type="math_number">
Expand All @@ -68,7 +92,13 @@
</block>

<block type="sprite_ball_get"></block>
<block type="sprite_ball_set"></block>
<block type="sprite_ball_set">
<value name="VALUE">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
<block type="sprite_ball_change">
<value name="DELTA">
<shadow type="math_number">
Expand All @@ -84,4 +114,21 @@

<block type="collision_get"></block>
</category>
<category name="Score" colour="#f2691e">
<block type="score_get"></block>
<block type="score_set">
<value name="VALUE">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
<block type="score_change">
<value name="DELTA">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
</category>
</xml>
3 changes: 2 additions & 1 deletion src/generators/bbasic.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,12 @@ import logic from './bbasic/logic';
import loops from './bbasic/loops';
import math from './bbasic/math';
import procedures from './bbasic/procedures';
import score from './bbasic/score';
import sprites from './bbasic/sprites';
import text from './bbasic/text';
import variables from './bbasic/variables';

[collision, colour, input, logic, loops, math, procedures, sprites, text, variables]
[collision, colour, input, logic, loops, math, procedures, sprites, score, text, variables]
.forEach((init) => init(Blockly));

export default Blockly.BBasic;
25 changes: 25 additions & 0 deletions src/generators/bbasic/score.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

export default (Blockly) => {
Blockly.BBasic[`score_get`] = function(block) {
// Score getter.
const code = 'score';
return [code, Blockly.BBasic.ORDER_ATOMIC];
};

Blockly.BBasic[`score_set`] = function(block) {
// Score setter.
const argument0 = Blockly.BBasic.valueToCode(block, 'VALUE',
Blockly.BBasic.ORDER_ASSIGNMENT) || '0';
return 'score = ' + argument0 + '\n';
};

Blockly.BBasic[`score_change`] = function(block) {
// Add value to the score.
const argument0 = Blockly.BBasic.valueToCode(block, 'DELTA',
Blockly.BBasic.ORDER_ASSIGNMENT) || '0';
const isNegativeConstant = /^\s*-\s*\d+\s*$/.test(argument0);
const operator = isNegativeConstant ? '' : '+';
return `score = score ${operator} ${argument0}\n`;
};
};

0 comments on commit 113a247

Please sign in to comment.