tweaked the code, added right side node selection algo

This commit is contained in:
ad044 2020-12-09 19:57:53 +04:00
parent 26e04ae838
commit 27ab0b18ac
3 changed files with 259 additions and 126 deletions

View file

@ -107,6 +107,7 @@ const NodeManager = (props: StateManagerProps) => {
if (props.eventState) { if (props.eventState) {
const eventAction = props.eventState.event; const eventAction = props.eventState.event;
const newActiveNodeId = props.eventState.newActiveNodeId; const newActiveNodeId = props.eventState.newActiveNodeId;
console.log(newActiveNodeId)
const newNodeRowIdx = props.eventState.newNodeRowIdx; const newNodeRowIdx = props.eventState.newNodeRowIdx;
const newNodeColIdx = props.eventState.newNodeColIdx; const newNodeColIdx = props.eventState.newNodeColIdx;
const newNodeMatIdx = props.eventState.newNodeMatIdx; const newNodeMatIdx = props.eventState.newNodeMatIdx;

View file

@ -45,9 +45,11 @@ const handleMainSceneEvent = (gameContext: any) => {
let newScene = gameContext.scene; let newScene = gameContext.scene;
if (subscene === "site") { if (subscene === "site") {
let selectedNodeData;
switch (keyPress) { switch (keyPress) {
case "LEFT": case "LEFT":
const selectedNodeData = nodeSelector({ case "RIGHT":
selectedNodeData = nodeSelector({
keyPress: keyPress, keyPress: keyPress,
nodeMatIdx: nodeMatIdx, nodeMatIdx: nodeMatIdx,
nodeColIdx: nodeColIdx, nodeColIdx: nodeColIdx,
@ -98,17 +100,6 @@ const handleMainSceneEvent = (gameContext: any) => {
event = "change_node"; event = "change_node";
} }
break; break;
case "RIGHT":
newNodeColIdx = nodeColIdx + 1;
if (newNodeColIdx > 3) {
event = "move_right";
newNodeMatIdx = newNodeMatIdx - 1 < 1 ? 8 : newNodeMatIdx - 1;
newNodeColIdx = 3;
newSiteRotY += -Math.PI / 4;
} else {
event = "change_node";
}
break;
case "CIRCLE": case "CIRCLE":
// in this case we have to check the type of the blue orb // in this case we have to check the type of the blue orb
// and dispatch an action depending on that, so we have to precalculate the // and dispatch an action depending on that, so we have to precalculate the
@ -154,7 +145,6 @@ const handleMainSceneEvent = (gameContext: any) => {
const newActiveHudId = const newActiveHudId =
hudAssocs[`${newNodeRowIdx}${newNodeColIdx}` as keyof typeof hudAssocs]; hudAssocs[`${newNodeRowIdx}${newNodeColIdx}` as keyof typeof hudAssocs];
console.log(newActiveNodeId)
return { return {
event: event, event: event,
newNodeColIdx: newNodeColIdx, newNodeColIdx: newNodeColIdx,

View file

@ -49,136 +49,278 @@ const isNodeVisible = (
} }
}; };
const nodeSelector = (context: NodeSelectorContext) => { const findNodeAfterMoving = (
const unlockedNodes = context.unlockedNodes; direction: string,
unlockedNodes: typeof unlocked_nodes,
level: string,
nodeMatIdx: number,
nodeRowIdx: number
) => {
let newNodeId;
let newNodeRowIdx = nodeRowIdx;
let newNodeMatIdx = nodeMatIdx;
let newNodeColIdx;
switch (context.keyPress) { if (direction === "left") {
case "LEFT": newNodeColIdx = 0;
let newNodeColIdx = context.nodeColIdx - 1;
if (newNodeColIdx < 0) {
const event = "move_left";
const newNodeMatIdx =
context.nodeMatIdx + 1 > 8 ? 1 : context.nodeMatIdx + 1;
const newSiteRotY = context.siteRotY + Math.PI / 4;
newNodeColIdx = 0; newNodeMatIdx = nodeMatIdx + 1 > 8 ? 1 : nodeMatIdx + 1;
let newNodeRowIdx = context.nodeRowIdx; newNodeId = getNodeId(level, newNodeMatIdx, nodeRowIdx, newNodeColIdx);
let newNodeId = getNodeId( let triedRows: number[] = [];
context.level,
newNodeMatIdx,
newNodeRowIdx,
newNodeColIdx
);
let triedRows: number[] = []; while (!isNodeVisible(newNodeId, unlockedNodes)) {
if (triedRows.length < 3) {
while (!isNodeVisible(newNodeId, unlockedNodes)) { triedRows.push(newNodeRowIdx);
if (triedRows.length < 3) { if (newNodeRowIdx === 1 && !triedRows.includes(0)) newNodeRowIdx = 0;
triedRows.push(newNodeRowIdx); else if (newNodeRowIdx === 1 && !triedRows.includes(2))
if (newNodeRowIdx === 1 && !triedRows.includes(0)) newNodeRowIdx = 2;
newNodeRowIdx = 0; else if (newNodeRowIdx === 2 && !triedRows.includes(0))
else if (newNodeRowIdx === 1 && !triedRows.includes(2)) newNodeRowIdx = 0;
newNodeRowIdx = 2; else if (newNodeRowIdx === 2 && !triedRows.includes(1))
else if (newNodeRowIdx === 2 && !triedRows.includes(0)) newNodeRowIdx = 1;
newNodeRowIdx = 0; else if (newNodeRowIdx === 0 && !triedRows.includes(1))
else if (newNodeRowIdx === 2 && !triedRows.includes(1)) newNodeRowIdx = 1;
newNodeRowIdx = 1; else if (newNodeRowIdx === 0 && !triedRows.includes(2))
else if (newNodeRowIdx === 0 && !triedRows.includes(1)) newNodeRowIdx = 2;
newNodeRowIdx = 1; } else {
else if (newNodeRowIdx === 0 && !triedRows.includes(2)) if (newNodeColIdx === 3) {
newNodeRowIdx = 2;
} else {
if (newNodeColIdx === 3) {
newNodeId = getNodeId(
context.level,
newNodeMatIdx,
newNodeRowIdx,
newNodeColIdx
);
} else {
newNodeColIdx++;
triedRows = [];
newNodeRowIdx = 0;
}
}
newNodeId = getNodeId( newNodeId = getNodeId(
context.level, level,
newNodeMatIdx, newNodeMatIdx,
newNodeRowIdx, newNodeRowIdx,
newNodeColIdx newNodeColIdx
); );
} else {
newNodeColIdx++;
triedRows = [];
newNodeRowIdx = 0;
} }
}
newNodeId = getNodeId(level, newNodeMatIdx, newNodeRowIdx, newNodeColIdx);
}
} else if (direction === "right") {
newNodeColIdx = 3;
newNodeMatIdx = nodeMatIdx - 1 < 1 ? 8 : nodeMatIdx - 1;
newNodeId = getNodeId(level, newNodeMatIdx, nodeRowIdx, newNodeColIdx);
let triedRows: number[] = [];
while (!isNodeVisible(newNodeId, unlockedNodes)) {
if (triedRows.length < 3) {
triedRows.push(newNodeRowIdx);
if (newNodeRowIdx === 1 && !triedRows.includes(0)) newNodeRowIdx = 0;
else if (newNodeRowIdx === 1 && !triedRows.includes(2))
newNodeRowIdx = 2;
else if (newNodeRowIdx === 2 && !triedRows.includes(0))
newNodeRowIdx = 0;
else if (newNodeRowIdx === 2 && !triedRows.includes(1))
newNodeRowIdx = 1;
else if (newNodeRowIdx === 0 && !triedRows.includes(1))
newNodeRowIdx = 1;
else if (newNodeRowIdx === 0 && !triedRows.includes(2))
newNodeRowIdx = 2;
} else {
if (newNodeColIdx === 0) {
newNodeId = getNodeId(
level,
newNodeMatIdx,
newNodeRowIdx,
newNodeColIdx
);
} else {
newNodeColIdx--;
triedRows = [];
newNodeRowIdx = 0;
}
}
newNodeId = getNodeId(level, newNodeMatIdx, newNodeRowIdx, newNodeColIdx);
}
}
return {
newNodeId: newNodeId,
newNodeMatIdx: newNodeMatIdx,
newNodeRowIdx: newNodeRowIdx,
newNodeColIdx: newNodeColIdx,
};
};
const findNode = (
direction: string,
unlockedNodes: typeof unlocked_nodes,
level: string,
nodeMatIdx: number,
nodeRowIdx: number,
nodeColIdx: number
) => {
let newNodeId;
let newNodeRowIdx = nodeRowIdx;
let newNodeMatIdx = nodeMatIdx;
let newNodeColIdx = nodeColIdx;
if (direction === "left") {
newNodeColIdx--;
let newNodeId = getNodeId(
level,
newNodeMatIdx,
newNodeRowIdx,
newNodeColIdx
);
let triedRows: number[] = [];
if (newNodeColIdx < 0)
return findNodeAfterMoving(
"left",
unlockedNodes,
level,
nodeMatIdx,
nodeRowIdx
);
while (!isNodeVisible(newNodeId, unlockedNodes)) {
if (triedRows.length < 3) {
triedRows.push(newNodeRowIdx);
if (newNodeRowIdx === 1 && !triedRows.includes(0)) newNodeRowIdx = 0;
else if (newNodeRowIdx === 1 && !triedRows.includes(2))
newNodeRowIdx = 2;
else if (newNodeRowIdx === 2 && !triedRows.includes(0))
newNodeRowIdx = 0;
else if (newNodeRowIdx === 2 && !triedRows.includes(1))
newNodeRowIdx = 1;
else if (newNodeRowIdx === 0 && !triedRows.includes(1))
newNodeRowIdx = 1;
else if (newNodeRowIdx === 0 && !triedRows.includes(2))
newNodeRowIdx = 2;
} else {
if (newNodeColIdx === 0) {
return findNodeAfterMoving(
"left",
unlockedNodes,
level,
nodeMatIdx,
nodeRowIdx
);
} else {
newNodeColIdx--;
triedRows = [];
newNodeRowIdx = 0;
}
}
newNodeId = getNodeId(level, newNodeMatIdx, newNodeRowIdx, newNodeColIdx);
}
} else if (direction === "right") {
newNodeColIdx++;
let newNodeId = getNodeId(
level,
newNodeMatIdx,
newNodeRowIdx,
newNodeColIdx
);
let triedRows: number[] = [];
if (newNodeColIdx > 3)
return findNodeAfterMoving(
"right",
unlockedNodes,
level,
nodeMatIdx,
nodeRowIdx
);
while (!isNodeVisible(newNodeId, unlockedNodes)) {
if (triedRows.length < 3) {
triedRows.push(newNodeRowIdx);
if (newNodeRowIdx === 1 && !triedRows.includes(0)) newNodeRowIdx = 0;
else if (newNodeRowIdx === 1 && !triedRows.includes(2))
newNodeRowIdx = 2;
else if (newNodeRowIdx === 2 && !triedRows.includes(0))
newNodeRowIdx = 0;
else if (newNodeRowIdx === 2 && !triedRows.includes(1))
newNodeRowIdx = 1;
else if (newNodeRowIdx === 0 && !triedRows.includes(1))
newNodeRowIdx = 1;
else if (newNodeRowIdx === 0 && !triedRows.includes(2))
newNodeRowIdx = 2;
} else {
if (newNodeColIdx === 3) {
return findNodeAfterMoving(
"right",
unlockedNodes,
level,
nodeMatIdx,
nodeRowIdx
);
} else {
newNodeColIdx++;
triedRows = [];
newNodeRowIdx = 0;
}
}
newNodeId = getNodeId(level, newNodeMatIdx, newNodeRowIdx, newNodeColIdx);
}
}
return {
newNodeId: newNodeId,
newNodeMatIdx: newNodeMatIdx,
newNodeRowIdx: newNodeRowIdx,
newNodeColIdx: newNodeColIdx,
};
};
const nodeSelector = (context: NodeSelectorContext) => {
let newNodeData;
switch (context.keyPress) {
case "LEFT":
newNodeData = findNode(
"left",
context.unlockedNodes,
context.level,
context.nodeMatIdx,
context.nodeRowIdx,
context.nodeColIdx
);
if (newNodeData) {
const didMove = context.nodeMatIdx !== newNodeData.newNodeMatIdx;
return { return {
event: event, event: didMove ? "move_left" : "change_node",
newNodeMatIdx: newNodeMatIdx, newNodeMatIdx: newNodeData.newNodeMatIdx,
newNodeRowIdx: newNodeRowIdx, newNodeRowIdx: newNodeData.newNodeRowIdx,
newNodeColIdx: newNodeColIdx, newNodeColIdx: newNodeData.newNodeColIdx,
newSiteRotY: newSiteRotY, newSiteRotY: didMove
? context.siteRotY + Math.PI / 4
: context.siteRotY,
newSitePosY: context.sitePosY, newSitePosY: context.sitePosY,
newLevel: context.level, newLevel: context.level,
}; };
} else { }
let event = "change_node"; break;
case "RIGHT":
let newNodeRowIdx = context.nodeRowIdx; newNodeData = findNode(
"right",
let newNodeMatIdx = context.nodeMatIdx; context.unlockedNodes,
let newSiteRotY = context.siteRotY; context.level,
context.nodeMatIdx,
let newNodeId = getNodeId( context.nodeRowIdx,
context.level, context.nodeColIdx
newNodeMatIdx, );
newNodeRowIdx,
newNodeColIdx
);
let triedRows: number[] = [];
while (!isNodeVisible(newNodeId, unlockedNodes)) {
if (triedRows.length < 3) {
triedRows.push(newNodeRowIdx);
if (newNodeRowIdx === 1 && !triedRows.includes(0))
newNodeRowIdx = 0;
else if (newNodeRowIdx === 1 && !triedRows.includes(2))
newNodeRowIdx = 2;
else if (newNodeRowIdx === 2 && !triedRows.includes(0))
newNodeRowIdx = 0;
else if (newNodeRowIdx === 2 && !triedRows.includes(1))
newNodeRowIdx = 1;
else if (newNodeRowIdx === 0 && !triedRows.includes(1))
newNodeRowIdx = 1;
else if (newNodeRowIdx === 0 && !triedRows.includes(2))
newNodeRowIdx = 2;
} else {
if (newNodeColIdx === 0) {
newNodeId = getNodeId(
context.level,
newNodeMatIdx,
newNodeRowIdx,
newNodeColIdx
);
} else {
newNodeColIdx--;
triedRows = [];
newNodeRowIdx = 0;
}
}
newNodeId = getNodeId(
context.level,
newNodeMatIdx,
newNodeRowIdx,
newNodeColIdx
);
}
if (newNodeData) {
const didMove = context.nodeMatIdx !== newNodeData.newNodeMatIdx;
return { return {
event: event, event: didMove ? "move_right" : "change_node",
newNodeMatIdx: newNodeMatIdx, newNodeMatIdx: newNodeData.newNodeMatIdx,
newNodeRowIdx: newNodeRowIdx, newNodeRowIdx: newNodeData.newNodeRowIdx,
newNodeColIdx: newNodeColIdx, newNodeColIdx: newNodeData.newNodeColIdx,
newSiteRotY: newSiteRotY, newSiteRotY: didMove
? context.siteRotY - Math.PI / 4
: context.siteRotY,
newSitePosY: context.sitePosY, newSitePosY: context.sitePosY,
newLevel: context.level, newLevel: context.level,
}; };