Commit 7798b4cf authored by treyzania's avatar treyzania

Added tool system, refactoring some frontend code.

parent faeea9ad
......@@ -38,6 +38,7 @@ const CAM_ZOOM_MULT = 1.05;
var cameraState = null;
var cameraTarget = null;
var viewCanvas = null;
var gWorldState = getStartingWorldCells();
var gEditWindow = null;
......@@ -50,6 +51,8 @@ var userToken = "notyetimplemented";
var mouse = null;
var keys = {};
var curToolName = null;
var curTool = null;
var userColor = Math.floor(Math.random() * COLORS.length);
var userDraw = []; // list of positions
......@@ -95,16 +98,14 @@ function init() {
screenCanvas = document.createElement("canvas");
// Controls
// FIXME This acts as if the player is typing. It's not a smooth flow.
window.onkeydown = handleKeyDown;
window.onkeyup = handleKeyUp;
let viewCanvas = document.getElementById("game");
viewCanvas = document.getElementById("game");
viewCanvas.addEventListener("click", handleClick);
viewCanvas.addEventListener("mousemove", function(e) {
let br = viewCanvas.getBoundingClientRect();
mouse.x = e.clientX - br.x;
mouse.y = e.clientY - br.top;
});
viewCanvas.addEventListener("mousemove", handleMouseMove);
// Now actually set up the starting tool.
switchToTool(defaultTool);
// Start render procedures.
updateDisplay();
......@@ -131,6 +132,30 @@ function init() {
}
function switchToTool(name) {
if (!TOOLS.hasOwnProperty(name)) {
console.log("Tried to switch to invalid tool: " + name);
return false;
}
console.log("Switching tools: " + name);
// Invoke the "switch from" handler.
if (curTool != undefined && curTool != null) {
curTool.onSwitchFrom(name);
}
// Switch out the handler.
curToolName = name;
curTool = TOOLS[name];
// Invoke the init function.
curTool.onSwitchTo()
return true;
}
function applyNewWorldState(s) {
console.log("got new whole world state (t = " + s.tick + ")");
......@@ -283,17 +308,29 @@ function handleKeyUp(e) {
function handleClick(e) {
let wc = convertScreenSpaceToWorldSpace(mouse);
let mx = Math.floor(wc.x);
let my = Math.floor(wc.y);
let tx = Math.floor(wc.x);
let ty = Math.floor(wc.y);
if (keys["shift"]) {
removeCellFromPending(mx, my);
} else {
for (let i = 0; i < userCurrentTemplate.length; i++) {
tryAddCellToPending(mx + userCurrentTemplate[i].x, my + userCurrentTemplate[i].y);
}
if (curTool != null) {
curTool.onClick(e, tx, ty);
}
}
function handleMouseMove(e) {
// Update some fields.
let br = viewCanvas.getBoundingClientRect();
mouse.x = e.clientX - br.x;
mouse.y = e.clientY - br.top;
// Now we *also* want to call out to the tool
if (curTool != null) {
let wc = convertScreenSpaceToWorldSpace(mouse);
let tx = Math.floor(wc.x);
let ty = Math.floor(wc.y);
curTool.onMouseMove(e, tx, ty)
}
}
function getStartingWorldCells() {
......@@ -318,48 +355,6 @@ function newBlankTile() {
}
}
function tryAddCellToPending(x, y) {
console.log("wanting to add " + x + " " + y + " to pending cells");
if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) {
console.log("out of bounds");
return;
}
// First check to see if we already have it.
for (let i = 0; i < userDraw.length; i++) {
if (userDraw[i].x == x && userDraw[i].y == y) {
return;
}
}
console.log("adding...");
// If not, then actually add it.
userDraw.push({
x: x,
y: y
});
// And update the UI.
updateSatoshiCostDisplay();
}
function removeCellFromPending(x, y) {
for (let i = 0; i < userDraw.length; i++) {
if (userDraw[i].x == x && userDraw[i].y == y) {
userDraw.splice(i, 1);
updateSatoshiCostDisplay();
return true;
}
}
return false;
}
function submitPendingCells() {
// Construct the list of updates, with the full settings.
......
var toolDraw = {
onSwitchTo: function() {
console.log("switched to draw tool");
},
onSwitchFrom: function(next) {
},
onMouseMove: function(e, tx, ty) {
},
onClick: function(e, tx, ty) {
if (keys["shift"]) {
removeCellFromPending(tx, ty);
} else {
for (let i = 0; i < userCurrentTemplate.length; i++) {
tryAddCellToPending(tx + userCurrentTemplate[i].x, ty + userCurrentTemplate[i].y);
}
}
}
}
var defaultTool = "draw";
var TOOLS = {
"nulltool": null,
"draw": toolDraw,
}
function tryAddCellToPending(x, y) {
console.log("wanting to add " + x + " " + y + " to pending cells");
if (x < 0 || x >= WORLD_WIDTH || y < 0 || y >= WORLD_HEIGHT) {
console.log("out of bounds");
return;
}
// First check to see if we already have it.
for (let i = 0; i < userDraw.length; i++) {
if (userDraw[i].x == x && userDraw[i].y == y) {
return;
}
}
console.log("adding...");
// If not, then actually add it.
userDraw.push({
x: x,
y: y
});
// And update the UI.
updateSatoshiCostDisplay();
}
function removeCellFromPending(x, y) {
for (let i = 0; i < userDraw.length; i++) {
if (userDraw[i].x == x && userDraw[i].y == y) {
userDraw.splice(i, 1);
updateSatoshiCostDisplay();
return true;
}
}
return false;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment