Skip to content

Commit

Permalink
Add new blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
bruh-9000 committed Mar 11, 2024
1 parent 1074d73 commit 70ef87b
Show file tree
Hide file tree
Showing 4 changed files with 398 additions and 655 deletions.
212 changes: 192 additions & 20 deletions hello-world/src/blocks/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,203 @@ import * as Blockly from 'blockly/core';
// text to the output div on the sample app.
// This is just an example and you should replace this with your
// own custom blocks.
const addText = {
'type': 'add_text',
'message0': 'Add text %1 with color %2',
'args0': [
{
'type': 'input_value',
'name': 'TEXT',
'check': 'String',

const secondTick = {
"type": "secondtick",
"message0": "secondTick",
"previousStatement": null,
"nextStatement": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const sendChatMessage = {
"type": "sendchatmessage",
"message0": "sendChatMessage %1",
"args0": [
{
"type": "input_value",
"name": "message",
"check": "String"
}
],
"previousStatement": null,
"nextStatement": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const triggers = {
"type": "triggers",
"message0": "Triggers:",
"inputsInline": true,
"nextStatement": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const script = {
"type": "script",
"message0": "Script:",
"inputsInline": true,
"nextStatement": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const ifelse = {
"type": "ifelse",
"message0": "if %1 do %2 else %3",
"args0": [
{
"type": "input_value",
"name": "check",
"check": "Boolean"
},
{
"type": "input_statement",
"name": "do1"
},
{
"type": "input_statement",
"name": "else1"
}
],
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const pos = {
"type": "pos",
"message0": "pos %1 %2 %3",
"args0": [
{
"type": "input_value",
"name": "x"
},
{
'type': 'input_value',
'name': 'COLOR',
'check': 'Colour',
"type": "input_dummy"
},
{
"type": "input_value",
"name": "y"
}
],
'previousStatement': null,
'nextStatement': null,
'colour': 160,
'tooltip': '',
'helpUrl': '',
"output": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const moveEntity = {
"type": "moveentity",
"message0": "moveEntity %1 %2 to pos %3",
"args0": [
{
"type": "input_value",
"name": "entity"
},
{
"type": "input_dummy"
},
{
"type": "input_value",
"name": "pos"
}
],
"previousStatement": null,
"nextStatement": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const triggeringPlayer = {
"type": "triggeringplayer",
"message0": "triggeringPlayer",
"output": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const owner = {
"type": "owner",
"message0": "owner",
"output": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const triggeringUnit = {
"type": "triggeringunit",
"message0": "triggeringUnit %1",
"args0": [
{
"type": "input_value",
"name": "NAME"
}
],
"output": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const selectedUnit = {
"type": "selectedunit",
"message0": "selectedUnit %1",
"args0": [
{
"type": "input_value",
"name": "NAME"
}
],
"output": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const frameTick = {
"type": "frametick",
"message0": "frameTick",
"previousStatement": null,
"nextStatement": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const serverShuttingDown = {
"type": "servershuttingdown",
"message0": "serverShuttingDown",
"previousStatement": null,
"nextStatement": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const gameStart = {
"type": "gamestart",
"message0": "gameStart",
"previousStatement": null,
"nextStatement": null,
"colour": 230,
"tooltip": "",
"helpUrl": ""
};

const test = {
"type": "test",
"message0": "test",
const onPostResponse = {
"type": "onpostresponse",
"message0": "onPostResponse",
"previousStatement": null,
"nextStatement": null,
"colour": 230,
Expand All @@ -46,4 +218,4 @@ const test = {
// This does not register their definitions with Blockly.
// This file has no side effects!
export const blocks = Blockly.common.createBlockDefinitionsFromJsonArray(
[addText, test]);
[script, secondTick, sendChatMessage, triggers, ifelse, pos, moveEntity, triggeringPlayer, owner, triggeringUnit, selectedUnit, onPostResponse, gameStart, serverShuttingDown, frameTick]);
117 changes: 99 additions & 18 deletions hello-world/src/generators/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,111 @@ import {Order} from 'blockly/javascript';
// This file has no side effects!
export const forBlock = Object.create(null);

forBlock['add_text'] = function (block, generator) {
const text = generator.valueToCode(block, 'TEXT', Order.NONE) || "''";
const color =
generator.valueToCode(block, 'COLOR', Order.ATOMIC) || "'#ffffff'";
forBlock['script'] = function (block, generator) {
// Generate the function call for this block.
const code = ``;
return code;
};

forBlock['triggers'] = function (block, generator) {
// Generate the function call for this block.
const code = `\n`;
return code;
};

forBlock['ifelse'] = function (block, generator) {
const check = generator.valueToCode(block, 'check', Order.NONE) || "";
const do1 = generator.valueToCode(block, 'do1', Order.NONE) || "";
const else1 = generator.valueToCode(block, 'else1', Order.NONE) || "";

// Generate the function call for this block.
let code;

if (else1) {
code = `if (${check}) {\n} else {\n}`;
} else {
code = `if (${check}) {\n}`;
}

return code;
};

forBlock['secondtick'] = function (block, generator) {
// Generate the function call for this block.
const code = `@secondTick\n`;
return code;
};

forBlock['sendchatmessage'] = function (block, generator) {
const text = generator.valueToCode(block, 'message', Order.NONE) || "''";

// Generate the function call for this block.
const code = `sendChatMessage(${text})\n`;
return code;
};

forBlock['pos'] = function (block, generator) {
const x = generator.valueToCode(block, 'x', Order.NONE) || "";
const y = generator.valueToCode(block, 'y', Order.NONE) || "";

// Generate the function call for this block.
const code = `pos(${x}, ${y})`;
return [code, generator.ORDER_NONE];
};

forBlock['moveentity'] = function (block, generator) {
const entity = generator.valueToCode(block, 'entity', Order.NONE) || "";
const pos = generator.valueToCode(block, 'pos', Order.NONE) || "";

const addText = generator.provideFunction_(
'addText',
`function ${generator.FUNCTION_NAME_PLACEHOLDER_}(text, color) {
// Generate the function call for this block.
const code = `moveEntity(${entity}, ${pos})\n`;
return code;
};

forBlock['owner'] = function (block, generator) {
// Generate the function call for this block.
const code = `.owner`;
return [code, generator.ORDER_NONE];
};

forBlock['triggeringplayer'] = function (block, generator) {
// Generate the function call for this block.
const code = `triggeringPlayer`;
return [code, generator.ORDER_NONE];
};

forBlock['triggeringunit'] = function (block, generator) {
// Generate the function call for this block.
const code = `triggeringunit`;
return [code, generator.ORDER_NONE];
};

forBlock['selectedunit'] = function (block, generator) {
// Generate the function call for this block.
const code = `selectedunit`;
return [code, generator.ORDER_NONE];
};

forBlock['frametick'] = function (block, generator) {
// Generate the function call for this block.
const code = `@frameTick\n`;
return code;
};

forBlock['servershuttingdown'] = function (block, generator) {
// Generate the function call for this block.
const code = `@serverShuttingDown\n`;
return code;
};

// Add text to the output area.
const outputDiv = document.getElementById('output');
const textEl = document.createElement('p');
textEl.innerText = text;
textEl.style.color = color;
outputDiv.appendChild(textEl);
}`
);
forBlock['gamestart'] = function (block, generator) {
// Generate the function call for this block.
const code = `${addText}(${text}, ${color});\n`;
const code = `@gameStart\n`;
return code;
};

forBlock['test'] = function (block, generator) {
forBlock['onPostResponse'] = function (block, generator) {
// Generate the function call for this block.
const code = `Test`;
const code = `@onPostResponse\n`;
return code;
};
5 changes: 3 additions & 2 deletions hello-world/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ const ws = Blockly.inject(blocklyDiv, {toolbox});
// generated code from the workspace, and evals the code.
// In a real application, you probably shouldn't use `eval`.
const runCode = () => {
const code = javascriptGenerator.workspaceToCode(ws);
const code = javascriptGenerator.workspaceToCode(ws).replace(/;\s*$/gm, '\n');

codeDiv.innerText = code;

outputDiv.innerHTML = '';

eval(code);
// eval(code);
};

// Load the initial state from storage and run the code.
Expand Down
Loading

0 comments on commit 70ef87b

Please sign in to comment.