This handle unit click is actually going to generate a method for each of them. So it's going to create a method that will be called when it's ready, where it will just dispatch an action to select the entity, the select entity action. It's just going to set a selected ID of whatever the current things that I've clicked on is. And we've just added a little bit of initial state here for null, which should be fine.
And now when we click on him, and with a little bit of extra CSS magic to show a bit of health in a select bar, we can now click on him and click off, and click on, and click off. Who wouldn't play that for several hours? Isn't that fantastic?
Now, if we want to be able to right click to move around, this very similar thing of which you can right click, have a right click handler, which this is what that on context menu method does. This is essentially your on right click method, if it had another name. And we're gonna do the same thing we did for the unit, where for every single tile, we're going to give it a handler, where we're going to handle the right click. We're going to generate a new method from the X and the Y, and dispatch this method. This is a little bit more complicated than the other one, but that's because we want to go and find the currently selected unit, and then basically tell it to move by taking the position from this new one we've just admitted, we've clicked on a tile, we wanna move there. And then we're gonna go through the units, update it, and reset the state.
So now click, boom, move. Oh, that's a bit fast. We don't really, that seems unfair, and doesn't seem too realistic for the medieval era to quite have teleportation yet. So, but because we're moving it just using CSS variables, we can set transition to just linear, and suddenly we've got this moving unit. Isn't that fantastic? Oh, actually. Well, now the problem with this is, it is moving smoothly, but irrelevant of how far he moves, it's gonna take one second, which is slightly too long. Which means that if he moves far away, it's super fast, and nearby, super slow. So how can we change this? Well, we could dynamically set that transition time distance based on how far he moved. So now, if you click very, very far away, the distance will be quite long. And if you click very short, it'll be quite a shorter time. So here's an example of that working, where now he's moving super far, and it's kind of slow, and he moves a little bit, and it's much faster. Much more consistent amount of time. But then, if you start to click around a bit quicker, it's not really working too much, and suddenly this isn't quite exactly what we want. You're clicking over here, you're clicking on one of them, and it's setting the position. You click on the next place, it speeds up the animation, but the unit hasn't actually moved that far. And also, actually, if we go back to that scrolling that was happening, it's actually only scrolling when you're moving the mouse. So if you move the mouse to the edge of the frame, and you don't actually wiggle it at all, it won't keep scrolling for you. This isn't really the behavior you want. And the reason why this is happening is because that idea I had around using that useEffect as a game loop, isn't really working, because the useEffect is only running when you actually move your mouse.
Comments