Monday, March 31, 2014

Unity Project Starbound Aces Combat System

Well, after a long break I decided to get back to my Senior Project ( seeing as I have to anyways ).

I always do better with a plan and chose to plan out our combat system for the game. The game logic for the system itself would not be difficult. Being a simple game, each ship is given a simple laser weapon.

The weapon itself was not hard to add to the game. Being something that travels in a straight line and is as fast as light, I was able to implement a simple raycast and line renderer for the weapon. The raycast is called when the mouse button is pressed down and a line rendering component is awoken upon firing to give some visual feedback. All in all a simple system. If the player hits somebody, they gather some required information and that information is sent off to the server. From there, SmartFox handles sending the messages to the rest of the players.

As far as code goes, I was having some difficulty implementing some of the features of this component. It seemed a lot of it had to do with stupid mistakes and timing issues. For instance, remote players joining rooms were not being instantiated properly and caused plenty of Null Pointer Exceptions. This was my bane for a week.

I decided to add a queue when a new player was added to the room, as opposed to adding the remote players instantly. Then I would wait for the level to load before adding the players in in the OnLevelWasLoaded method. If the players were added after the client is in the room, they are queued and polled in the FixedUpdate method.

In the FixedUpdate method the count of queued players is checked, if there are any players, they are immediately added to the scene and their information filled out. They are then added to the remote players list.

After fixing all of these issues and correcting a lot of timings, I was able to continue on with the combat scripts. I added a simple script called Weapon, which in later iterations will be abstract to allow for well implemented inheritance to add different types of weapons.

Anyways, this script is small, sweet, and simple. It projects a raycast from the origin of the player. If it hits anything, it checks to see if it is another player. If it is another player, the damage that the client can do and the server id of the player that was hit is sent as an extension request to SmartFox. All players are then notified of this event. The notified players check to see if it is their Id that was sent. If so, their health is reduced by the damage that the original shooter causes.

In hindsight, the server needs to start picking up more responsibility. Once the development picks up, I will ensure this happens. The reason I am saying that the server needs more responsibility is that I can never trust the client. I need the server to determine whether a player is actually able to shoot and if they in fact hit the player that they said they did.

All in all, it was an okay day for development and I added a new feature. Being complete, I pushed the source code to the repository for all to see. I do acknowledge that it needs to be cleaned up and re-factored, but that will come with time, if there is any left.

No comments:

Post a Comment