• Examples (iOS)
• Examples (Android)
• Examples (C++)
Server API Documentation


» SpaceWar

» Overview

The SpaceWar example is a tribute to to the homonymous game developed in the 60s, one of the earliest computer games in history! The purpose of this example is to showcase the capabilities of SmartFoxServer 2X MMO Rooms in a realtime game featuring flying starships, weapon shots, collisions... with the possibility of having thousands of them in the same Room.

This example is a porting of its Flash counterpart. The two versions of the game share the same server-side Extension, which contains the core logic of the game, and a similar approach in client development. Also, the game flow and basic concepts like clients synchronization and Room configuration (but not only) are the same for the two versions. For this reason we strongly recommend that you read the in-depth Flash tutorial to learn the multiplayer programming techniques used in the Unity game too.

This example makes use of the new 2D Tools introduced with Unity 4.3, in particular the new sprite renderers and animators. Before Unity 4.3, making 2D games was still possible, but the process was quite long, which led to many plugins helping in the workflow appear on the Asset Store.
With the new 2D tools, porting the original Flash example (ActionScript 3.0 language) to Unity engine was a much simpler task. Most of the code was reused, differing mostly on Unity’s renderer specific tasks and on the menu controller.

The next paragraphs will draw your attention on a few steps of the 2D development process specific to Unity. Again, you should read the Flash tutorial for an extended explanation of the core game logic.
Also check the comments to methods and properties in the source code for additional informations and possible code optimizations.

>> DOWNLOAD the source files <<

» Setup & run

In order to setup and run the example, follow these steps:

  1. copy the SpaceWar.zone.xml file (containing the Zone configuration) from the /server/Zone folder to your SFS2X installation folder, under /SFS2X/zones;
  2. copy the /server/Extension_Java/deploy/SpaceWar folder to the server's /SFS2X/extensions folder;
  3. start SmartFoxServer 2X (v2.13 or later is highly recommended);
  4. start Unity (v5.6 or later required);
  5. in the Projects panel click on the Open icon and browse to the /client/SpaceWar folder, then click the Open button;
  6. wait for the project setup completion (Unity needs to regenerate some libraries);
  7. go to the Project panel, click on the Assets/Scenes folder and double click on one of the Gamescene to open it;
  8. if SmartFoxServer and Unity are not running on the same machine, change the IP address of the server in the inspector of the Main Camera game object in the scene;
  9. click on the Play button to run the example in the Unity Editor, or go to the Build settings panel and build it for your target platform of choice.

All relevant client assets are contained in the Assets folder; in particular the C# code is in the Scripts subfolder and the SmartFoxServer 2X client API DLLs are in the Plugins subfolder. Read the introduction to understand why multiple DLLs are used.

» Extension

The game features a server-side Extension, as described in the overview. The Extension is available in two versions: Java and JavaScript.

Step 2 above shows how to deploy the Java Extension, which is the default one for the example. In order to access its code, create and setup a new project in your Java IDE of choice as described in the Writing the first Java Extension document of the Java Extensions Development section. Copy the content of the /server/Extension_Java/source/src folder to your Java project's source folder.

In order to use the JavaScript Extension, in step 2 copy the /server/Extension_JavaScript/deploy/SpaceWar-JS folder to the server's /SFS2X/extensions folder. Then, open the Zone configuration file (see step 1) and change it according to the following:

Please note that the description of the Extension code in the Flash tutorial mentioned in the overview refers to the Java version of the server side Extension. The same logic (in a different language) is coded in the JavaScript Extension.

» Client highlights

» SpriteSheet creation

Before creating the sprites for the game elements, first we have to create the assets' SpriteSheet.

In the Images folder you'll find a SpriteSheet with all the animated elements plus separated SpriteSheets for each static element. All of these assets were obtained from the original Flash example's SpriteSheet.

To create a SpriteSheet in Unity, you have to select your SpriteSheet image and, on the Inspector, set its Texture Type to Sprite and configure its Sprite Mode according to the SpriteSheet you're adding: Single for a single framed SpriteSheet or Multiple, in case it's a SpriteSheet with multiple elements.

In a Multiple SpriteSheet, you also have to specify each element's frames. Thankfully Unity deals with most of the work, and you just need to open the Sprite Editor, open the Slice menu, at the top left, and set the Type to Automatic. You'll notice that, on the Project Panel, there's an arrow at the left of your SpriteSheet. If you open it, you'll have access to the different frames.

After generating the Multiple SpriteSheet, you'll want to specify the animations of each element. In order to achieve that, simply select the corresponding frames and drag them onto the Scene. Unity will then prompt you to create an AnimationClip and an AnimatorController for the selected frames, which you can later add to the prefab.

» The Animator Controller

In the Animations folder you'll find folders with the animation clips and controllers for the different animated game elements. These assets were generated by manually selecting the corresponding frames and adding them to the Scene.

When you double click one of the AnimatorController, you'll be presented with Unity's new Animator Panel. Here you'll be able to customize the animations' sequence and duration, alongside with the necessary triggers, which will be used in the code for triggering each animation.

» Controlling the menu

While the Flash example controls the menu through multiple screens, on the Unity Example makes use of Unity's UI API, which is all controlled by the Main class.

» The FPS counter

Contrary to the Starling engine used in the Flash example, Unity doesn't come with an embedded Frames-Per-Second counter. You can find multiple implementations online. We used the one from Unity's wiki: http://wiki.unity3d.com/index.php?title=FramesPerSecond#CSharp_HUDFPS.cs

» More resources

You can learn more about the MMO Room features by consulting the following resources: