Rust Shooter
Technologies Used:
-
Rust
-
crate::specs
-
SDL
What I have done:
-
Created Player/Asteroids/Rocket entities using Entity Component System
-
Created a Position, Renderabe components.
-
Created movement Logic, which changes the positions of objects
-
Created a Texture Manager that handles the loading and using of the textures
-
Created an Input Manager which handles the registration and usage of keys
-
Created the collision detection for Rocket, Player and Asteroid entities
What can be improved:
-
Changing the collision logic to AABB
-
VS Code
GitHub Page:
Details:
This is a performance aware asteroids game clone. This project has a data oriented structure. Also the language I used in this project is Rust, since it felt like a good opportunity to learn it. Performance testing of this project is done by creating an fps counter and also using Visual Studio's profiler.This project also has ECS(Entity Component System) and for that I'm using the crate specs. I focused on this project for 3 weeks.
The first itteration of the project has very low fps, one reason is I used to create the textures for entites in every frame. To fix this I have created a Texture Manager which creates the textures at the start and keeps them in a container. When needed I called the textures from said container.
Another reason is the sdl's way of handling the UI, in order to have a dynamic UI in sdl I created their textures from text surface everyframe, which is also very performance heavy operation. To fix this I kept the existing UI elements in a list and refreshed the list every 100 frames, which increased the performance nearly %400.
Also every frame, for each entity, I'm checking if they collided with the existing entities in the world. This has an exponential time complexity. This can be avoided if we use an AABB trees for collision checking, the simplest way to implement is to separate the game scene into grids and checking collision inside these grids. However we atmost going to have one player so this is not that neccesary fix.
As components for Entity Component system (ECS) I have: Position, Renderable, Player, Asteroid, Rocket, Pending Asteroid and Game data
-
Rendereable: Stores the variables used by SDL for render able objects
-
Position: Stores the x and y positions in the screen
-
Player: Has speed, direction and determines if the player can take damage
-
Asteroid: Has speed, rotation speed and size of the asteroid
-
Rocket: Has speed
-
Pending Asteroid: Has position , rotation and size of the asteroid that’s gonna be created as childs of the original one that is destroyed
-
GameData: Has the level and score of the game.
During the first run of the game, there was a fps drop and using Visual studio profiler, I noticed the biggest reason of the performance problem is SDL's rendering function. To solve this issue I used two different methods first is keeping all the entities that has a sprite in a texture manager. This texture manager will load the sprites at the start of the program and is going to keep all the textures in a hash map using a string as key, and when something needs to be rendered to the screen, they will use the key to get the corresponding texture from the map. However this solution does not work for the UI elements since they need to be updated when change occurs. To fix this, I have created a list which holds the current UI elements and updated the list every 100 frames, doing so increased the performance by %400
One another thing to note is in this project, the collisions are done using two for loops, the nested loop compares the positions of the each object in the scene and the first loop changes the compared object.(i.e: player with asteroids) this operation have quadratic time complexity which is bad however, since we are going to have at most 1 player or at most 5 rockets in the scene, this operation actually tooks linear time complexity if we have n asteroids. To improve this different methods like, AABB collision checking can be used.