Start your engines!

Let's move our player ship around the map!

I imported the mesh I created in Blender into Unreal; then, as usual, I created a PlayerShipClass based on AActor, to manage the behaviour of my ship.

 Instead of dragging it in the editor to create my Static Mesh Actor, I'm going to spawn it from code, inside the BeginPlay() method of my PlayerController class:

Here I can specify the world position of my mesh, and this will become necessary when we have multiple levels, each with its own map dimensions. Then I set the gameState to 0, meaning that the game will start inside the Player turn, waiting for his input.

Let's take a look at the PlayerShipClass constructor, where I'm specifying what mesh to use:


As we did with Hexagon tiles, I'm telling the constructor where to find the mesh, assigning it to the Actor and setting its location and scale.

Let's compile, and take a look at our glorious spaceship, awaiting for orders at the center of the map:




Amazing! Now, I want to see some movement.

The player ship is going to move after the user clicks on a tile. If you remember, we created a method to handle mouse clicking in this post, so all we need to do now is fill up the method MouseClicked():


Here we're getting the X and Y coordinates of our actualSelection object (see here for its definition), and confronting them with the ship coordinates. If they're not the same, we need to move to the new position. If the tile is Reachable from the player's position (see the code below to see how I find it out), I simply call the setDestination(x, y) method of the ship, and let it handle its own movement logic.



I also set the gameState to 1, meaning that the player turn is over, and we are in the player movement state.

In the PlayerShipClass header file I created to utility constants: the first one (directionRotation) is an array associating the direction to angles:

The second one (directionMap) is a matrix that associates the current direction to the target direction, deciding if the rotation should be clockwise or counter-clockwise. Here are both:


Ok, let's see what setDestination() does:


 It simply declares that the ship is moving, and then compares the destination coordinates with its current ones, setting a direction for the ship. That's it, we'll handle the actual movement in the tick() method of the PlayerShipClass, so that it will be animated instead of instantly teleport from one place to another. This is the tick() method:


The logic is activated only if the ship is moving. If we're already in the right direction, we can start to move, else we need to rotate first. Here's a screenshot of the two methods move() and rotate():



They basically use the SetActorLocation() and SetActorRotation() methods to move the Actor, using the MovingSpeed and RotationSpeed multiplied by DeltaTime (it's the time between 2 frames, in milliseconds) to animate it. When the ship is within a range of the target location (here I use 5 units), we simply declare that the target is reached, and we stop the moving of the ship:


And we've done our first animation, the game is finally coming to life!

If you look at the game loop in my previous post, you'll see that the game state will now go to 2 (AI turn), then to 3 (AI movement) and back to 0, so the turns cycle can start again and we're allowed to click on another cell and see the ship move there.

Thanks for reading, and game you next time!

Commenti