Skip to content

Commit

Permalink
Complete Earth missions
Browse files Browse the repository at this point in the history
  • Loading branch information
brianyu28 committed Apr 18, 2021
1 parent e2ca55d commit da2b7a3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/components/Level.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ const Level = ({ planetIndex, missionIndex, onSuccess, onFailure, program, progr

// Decide which instruction to use
let obj;
let instr;
switch (instruction.block) {

// Move the rover forward
Expand Down Expand Up @@ -342,10 +343,11 @@ const Level = ({ planetIndex, missionIndex, onSuccess, onFailure, program, progr
items: roverNoop(state.items),
}));
} else {
instr = instruction.meta.elseJump === undefined ? instruction.meta.jumpTo : instruction.meta.elseJump;
setState(state => ({
...state,
currentInstruction: instruction.meta.jumpTo - 1,
instructionsCompleted: instruction.meta.jumpTo,
currentInstruction: instr - 1,
instructionsCompleted: instr,
items: roverNoop(state.items),
}));
}
Expand All @@ -360,15 +362,26 @@ const Level = ({ planetIndex, missionIndex, onSuccess, onFailure, program, progr
items: roverNoop(state.items),
}));
} else {
instr = instruction.meta.elseJump === undefined ? instruction.meta.jumpTo : instruction.meta.elseJump;
setState(state => ({
...state,
currentInstruction: instruction.meta.jumpTo - 1,
instructionsCompleted: instruction.meta.jumpTo,
currentInstruction: instr - 1,
instructionsCompleted: instr,
items: roverNoop(state.items),
}));
}
break;

case BLOCK_NAMES.ELSE:
instr = instruction.meta.jumpTo;
setState(state => ({
...state,
currentInstruction: instr - 1,
instructionsCompleted: instr,
items: roverNoop(state.items),
}));
break;

case BLOCK_NAMES.END_IF:
setState(state => ({
...state,
Expand Down
24 changes: 24 additions & 0 deletions src/game/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const BLOCK_NAMES = {
END_REPEAT: 'END REPEAT',
IF_BUTTON_BLUE: 'IF_BUTTON_BLUE',
IF_CARRYING_BLUE: 'IF_CARRYING_BLUE',
ELSE: 'ELSE',
END_IF: 'END_IF',
};

Expand Down Expand Up @@ -60,6 +61,9 @@ export const BLOCKS = {
[BLOCK_NAMES.IF_CARRYING_BLUE]: {
name: 'If Carrying Blue'
},
[BLOCK_NAMES.ELSE]: {
name: 'Else'
},
[BLOCK_NAMES.END_IF]: {
name: 'End If'
},
Expand Down Expand Up @@ -137,6 +141,22 @@ export const validate_program = (program) => {
stack.push({type: instruction.block, condition: true, line: i});
break;

case BLOCK_NAMES.ELSE:
top = stack.pop();
if (top === undefined) {
return {isValid: false, error: 'Tenacity can only have an Else after a matching If instruction.'};
}
else if (top.condition !== true) {
return {isValid: false, error: 'Tenacity tried to Else, but it looks like some other part of the program needs to be resolved first.'};
}
else if (augmentedProgram[top.line].meta.elseJump !== undefined) {
return {isValid: false, error: 'An If instruction can only have one Else expression.'};
}
augmentedProgram[top.line].meta.elseIndex = i;
augmentedProgram[top.line].meta.elseJump = i + 1;
stack.push(top);
break;

case BLOCK_NAMES.END_IF:
top = stack.pop();
if (top === undefined) {
Expand All @@ -146,6 +166,10 @@ export const validate_program = (program) => {
return {isValid: false, error: 'Tenacity tried to End If, but it looks like some other part of the program needs to be resolved first.'};
}
augmentedProgram[top.line].meta.jumpTo = i + 1;
const elseIndex = augmentedProgram[top.line].meta.elseIndex;
if (elseIndex !== undefined) {
augmentedProgram[elseIndex].meta.jumpTo = i + 1;
}
break;

default:
Expand Down
62 changes: 62 additions & 0 deletions src/game/missions.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,69 @@ export const PLANETS = [
criteria: [
{category: 'location_x', id: 'rock', value: 500, message: 'Rock sample did not end up in blue box.'},
]
},
{
objective: 'There is one rock sample in the "Samples" box. Move it to the appropriately labeled color box.',
blocks: [
[BLOCK_NAMES.FORWARD, 5],
[BLOCK_NAMES.PICK_UP, 1],
[BLOCK_NAMES.DROP, 1],
[BLOCK_NAMES.IF_CARRYING_BLUE, 1],
[BLOCK_NAMES.ELSE, 1],
[BLOCK_NAMES.END_IF, 1],
],
items: [
{...d, id: 'rock', object: OBJECTS.ROCK_RED, x: 200, elevation: -50, canCarry: true, color: 'red'},
{...d, id: 'box_samples', object: OBJECTS.BOX_SAMPLES, x: 200, elevation: -100},
{...d, id: 'box_red', object: OBJECTS.BOX_RED, x: 500, elevation: -100},
{...d, id: 'box_blue', object: OBJECTS.BOX_BLUE, x: 400, elevation: -100},
{...d, id: 'rover', object: OBJECTS.ROVER, x: 200},
],
criteria: [
{category: 'location_x', id: 'rock', value: 500, message: 'Rock sample did not end up in red box.'},
]
},
{
objective: 'There are three rock sample in the "Samples" box. Move each of them to the appropriately labeled color box.',
blocks: [
[BLOCK_NAMES.FORWARD, 7],
[BLOCK_NAMES.TURN, 4],
[BLOCK_NAMES.PICK_UP, 1],
[BLOCK_NAMES.DROP, 2],
[BLOCK_NAMES.IF_CARRYING_BLUE, 1],
[BLOCK_NAMES.ELSE, 1],
[BLOCK_NAMES.END_IF, 1],
[BLOCK_NAMES.REPEAT, 1],
[BLOCK_NAMES.END_REPEAT, 1],
],
items: [
{...d, id: 'rock1', object: OBJECTS.ROCK_RED, x: 200, elevation: -50, canCarry: true, color: 'red'},
{...d, id: 'rock2', object: OBJECTS.ROCK_BLUE, x: 200, elevation: -51, canCarry: true, color: 'blue'},
{...d, id: 'rock3', object: OBJECTS.ROCK_RED, x: 200, elevation: -52, canCarry: true, color: 'red'},
{...d, id: 'box_samples', object: OBJECTS.BOX_SAMPLES, x: 200, elevation: -100},
{...d, id: 'box_red', object: OBJECTS.BOX_RED, x: 400, elevation: -100},
{...d, id: 'box_blue', object: OBJECTS.BOX_BLUE, x: 500, elevation: -100},
{...d, id: 'rover', object: OBJECTS.ROVER, x: 200},
],
criteria: [
{category: 'location_x', id: 'rock1', value: 400, message: 'Red rock sample did not end up in red box.'},
{category: 'location_x', id: 'rock2', value: 500, message: 'Blue rock sample did not end up in blue box.'},
{category: 'location_x', id: 'rock3', value: 400, message: 'Red rock sample did not end up in red box.'},
]
},
{
objective: "We're done here on Earth! Launch the rocket to head to the next planet.",
blocks: [
[BLOCK_NAMES.LAUNCH_ROCKET, 1]
],
items: [
{...d, id: 'rocket', object: OBJECTS.ROCKET, x: 400},
{...d, id: 'rover', object: OBJECTS.ROVER, x: 400},
],
criteria: [
{category: 'event', value: EVENTS.ROCKET_LAUNCH, message: 'Rocket did not launch.'}
]
}
]
},
]

0 comments on commit da2b7a3

Please sign in to comment.