Space War Fare MMO Interest Management Source Code
Introduction
This page describes the source code functions written for the topic discussed in Space War Fare MMO Interest Management. Please go through that before proceeding further.
The complete source code (server side as well as AS3 client side) is available on our download page in the samples section.
The game has a big world divided into smaller regions. Each player has an area of interest, which depicts what is the area, in which the player is interested in getting the updates from.
Whenever a player joins the game, we add him to the world and whenever he moves, we calculate the regions that fall under his interest area and subscribe him to that region. Then the player multicast the chat message in his region, and that message is then sent to all players who are subscribed to that region.
Implementation
To implement our MMO logic, we create 3 major classes
- Player
- Region
- World
Player represents individuals present in the world. World is an area containing those all players. Region is subarea of the world. World is composed of these regions.
There are three more classes to make mathematical representation easy
- VectorI
- VecotrF
- Rect
VectorI represents a vector quantity with each component represented by an Integer whereas VectorF represents a vector quantity with floating value components. Rect is used for rectangles. I has two components Origin and Size. Both are of type VectorF.
World.java
World represents a big area that contains all the players. World is divided into regions
public class World { /* * Initialises the World * @param room IRoom to which the world is attached to * @param TopLeft This represents the coordinates of Top Left point * @param BottomRight This represents the coordinates of Bottom Right point * @param SubDivisions This is the number of regions the world has to be divided into */ public World(IRoom room,VectorF TopLeft, VectorF BottomRight, VectorI SubDivisions); /* * Returns a region that contains the (x,y) point * @param x * @param y */ public Region findRegion(float x, float y); /* * Adds a player to the world * @param p Player that is being added to world */ public void addPlayer(Player p); /* * Removes a player from the world * @param p Player that has to be removed */ public void removePlayer(Player p); /* * Get the player reference from the IUser reference * @param user IUser reference to the user */ public Player getPlayer(IUser user); /* * Updates the player instance in the world * @param p Player that has to be updated in the world * @param removedForPlayers Reference to the list, which will contain the list of all players from whom's interest area the Player p has been removed * @param removedPlayer Reference to the list, which will contain the list of all player that has left the player p's interest area * @param addedPlayer Reference to the list, which will contain the list of all player that has entered the player p's interest area * @return void */ public void updatePlayer(Player p, List<Player> removedForPlayers, List<Player> removedPlayers, List<Player> addedPlayers); /* * Send Message to all the players subscribed to the Player p's current region * @param p Player who is sending the message * @param msg Message to be sent */ public void multicastMessage(Player p, String msg); /* * Send a messaged to a list of players * @param sender Player who is sending the message * @param receivers List of players who will be receiving the message * @param msg Message to be sent * @return void */ public void multicastMessage(Player sender, List<Player> receivers, String msg); /* * Send a message to a player * @param receiver Player who will recieve the message * @param message Message to be sent * @param sender Player who is sending the message */ public void sendMessage(Player receiver, String message, Player sender); }
Player.java
Players represents the individuals that are present in the world.
public class Player { /* * Initialize the Player with position as (0,0) * @param user Reference to IUser */ public Player(IUser user); /* * Initialize the Player with position as (x,y) * @param user Reference to IUser * @param x * @param y */ public Player(IUser user,float x, float y); /* * Initialize the Player with position as pos * @param user Reference to IUser * @param pos Vector representing the position */ public Player(IUser user,VectorF pos); /* * Sets the dimensions of interest area of player * @param rc Dimensions of the rectangle representing area of interest */ public void setInterestAreaSize(VectorF rc); /* * Sets the dimension of interest area of player * @param width Width of the rectangle representing area of interest * @param height Height of the rectangle representing area of interest */ public void setInterestAreaSize(float width, float height); /* * Returns the area of interest size */ public VectorF getInterestAreaSize(); /* * Returns the IUser reference of player */ public IUser getIUser(); /* * Return the position of player */ public VectorF getPosition(); /* * Sets the position of player * @param x * @param y */ public void setPosition(float x, float y); /* * Returns the reference to the current region where player is present */ public Region getRegion(); /* * Set the reference to current region */ public void setRegion(Region rgn); /* * Returns the list of all regions who will publish updates to the player */ public List<Region> getPublishers(); /* * Returns the name of player */ public String getName(); }
Region.java
Region is a subarea of the world. It maintains a list of all players that fall inside that region along with all the players that are subscribed to that region.
public class Region { /* * Initializes the Region * @param x x-coordinate of position * @param y y-coordinate of position * @param width width of the Region * @param height height of the Region */ public Region(float x, float y, float width, float height); /* * Adds a subscriber to the region * A subscriber may not present inside the region * Subscriber is the one who wants to listen to the region * @param p Player that has to be added */ public void AddSubscriber(Player p);scribers.add(p); } /* * Unsubscribe a player from the region by removing him * @param p Player that has to be removed from subscription */ public void RemoveSubscriber(Player p); /* * Add a player inside the region * @param p Player that has to be added */ public void AddPlayer(Player p); /* * Remove the player from the region * @param p Player that has to be removed */ public void RemovePlayer(Player p); /* * Returns the list of all Players */ public List<Player> getPlayers(); /* * Returns the list of all the subscribers */ public List<Player> getSubscribers(); //Returns the rectangle representing the region public Rect getRect(); }