If you've never used 3 before, don't worry. This is like in CSS when you have a transform, you have position, rotation, scale. We're just making sure that that data is linked with our traits. But that alone does not get us anything in our game.
We have to start spawning stuff. Now we need to write some game logic. We're going to create our first action. Our first actions here are going to be one for spawning players. We're going to spawn entities that have the player trait and transform. And then spawning enemies spawn entities that have the enemy trait and transform.
When we use it in React here on startup, on mount, we're just calling it in effect spawn player. Then we're setting an interval for spawning an enemy every second. We're adding startup to our app. And we get this. Now we have our player and we have our enemies, but they're all spawning bunched up on top of each other, so we want to do something about that.
We're going to go back to our enemy action. We're going to make sure that when we spawn our enemy, we are creating a random position and rotation. That way every time that action is called, we know that they're getting placed randomly around the map. And it's getting passed into the trait as a default prop.
All right. Now we get what we want. Every time that action is called in the interval, we are getting them placed randomly around our game level. Now let's talk about behavior. We've got them spawning, but we want them doing things. We want to make the game. Very important here is that we get the break, one of the most fundamental rules of React.
If you do a set state every frame, people would say your React app is broken, and it truly would be. With Kota, though, in order to get things to come alive so that every time you look at the screen, every frame, it's moving, it's doing something new, we need to make sure that we're updating everything every frame.
Comments