This week I have done The final AI-state “AttractedState” and I have started wrestling with the mixture of three super intelligent coding chefs in this soup of code. It’s lovely but it’s like fitting a cube inside the point of a needle.
Let’s start out with the attracted state.
This is a hunting state as explained in the previous post HERE but instead of a unit vector pointing from the enemy to the player fish, there is a vector pointing to the center of the light bulb. As of now, all fishes swim towards the light bulb when it’s lit. When the light bulb is not lit, the grey fishes flee to their “safe” distance and return to idle. The green fish are also attracted to the light but if they are close and you turn of the light, they enter hunting state. Hunting state has been updated so now when they collide with the player fish they take a bite from the player fish (dealing it damage) and flee until they are at the “safe” distance and return to idle. I believe this way is the final AI behavior with perhaps some tweaking in numbers deciding from what distance these behaviors are triggered.
The thing I am working on right now is the color coding of the fishes. The enemy fishes are supposed to have a glowing aura around them letting the player know instantly if they can eat it or if he or she should run away in shear panic while trying not to soil their pants.
If the fish has a green glow about it, it is smaller than the player’s fish and this means you can eat them. If they have a yellow glow, the player knows they are the same size, this means you can eat them and they can deal quite a bit of damage to the player’s fish.
If they have a red glow about them, the player should consider turning of the computer or risk suffering a heart attack in a primitive form of absolute panic – it means the fishes are bigger then the player’s fish! They can kill the player’s fish in a blink of an eye.
That is why and what. How is still on the drawing board.
What I’ve done so far is I am using the built in SFML VertexArray with four elements creating a rectangle. then I need to have a texture, this is also a built in class of SFML. This texture is drawn out separately but using the same position as the vertrices using the SFML’s window. The texture also uses four coordinates in the sprite sheet I use for the glowing image. This image is a transparent picture with white outlines of the fishes. this allows me to make them change colors. The Vertex array can set any color(s) to it’s shape.
I can say I wasn’t really sure where to start making these vertrices or how to fit them perfectly to the fishes since they are in different sizes as compared to the actual size in the sprite sheet. Because I use vertrices, who’s coordinates are defined at each of its corners and not in their top left corner or a specified origin such as sprites are, I need four coordinates to move it around the map. The very first thing that came into my head was the global boundaries of the sprites. Global boundaries is yet another feature of SFML. It returns a floatrect (or perhaps it’s an intrect) that gives you the coordinates – left, top, width and height of the image in question. That didn’t work out for some reason, probably because I overlooked something. In the process of doing and learning my responsibilities in a early stage of the production and because of some changes in the way we finally decided to handle our sprites, I missed a little information.
The colliders on the other hand I know inside and out. So I used the position of the colliders to update the vertrix. The colliders are placed on the sprites originating from the center of the sprite. From this information I can get all the corners. The left corner from the center of the collider is exactly the center position’s X-value minus the half of the width and the center position’s Y-value minus half its height. In the same manner you can get all the four points needed.
Just look at this wonderfully perfect fish and its glow:
The only problem right now is that the glow does not turn with the fish and that’s sort of annoying. I have three different theories that I will try after this blog.
Either I need to change the order in which the vertex array’s elements are created. In other words instead of from the top left corner to the right top corner to the right bottom corner to the left bottom corner, to draw it from the top right to the top left to the bottom left and to the bottom right. This could make the image inverted.
or I could change the texture coordinates to emerge in the same inverted order.
Or I need to do both. One of these ways should work out. I know the order does matter because I had a problem with the picture being all wrong when I had one coordinate mixed up with another.
So there you have it.