Writing · 2018 · 1 min
Unit Manager Code Snippet
Just about every strategy game needs a way to manage all the units on the field. Like all the enemy units, all of the player’s units and even neutral entities.
That also means a way for you to easily identify which unit is friendly with each other or enemies.
So here is the pattern I currently use for Riflestorm.
It contains three classes:
- TeamList - Contains all three teams (or how many you want) with helper methods. Only one is needed.
- Team - Contains all the team members with helper methods.
- Entity - This is the individual unit. It’s a dumby file just to illustrate how it’s used.
Usage
// create one teamlist for the whole game
TeamList teamList = ;
// teams are automatically made
teamList.misc; // neutral team
teamList.teamOne; // team one
teamList.teamTwo; // team two
// create an entity and add it to the class
GameObject entityObj = ;
Entity entity = entityObj.;
// add to team one
teamList.teamOne.;
// entity now has access to it's team
entity.team; // teamList.teamOne
entity.teamId; // can check index in team
// can find entities in the world
teamList.; // entity
// or find based on team
teamList.teamOne.; // entity
// entities has it's team instance
entitiy.team.Count; // 1
// can even get the team list instance
entity.team.teamList.; // null
// can also check if an other entity is friendly
if
// can do checks on the team like
teamList.teamOne.; // false
// or check if at least one is at a location
teamList.teamOne.; // true
Handling The Player’s Team
One thing you’ll notice is the teams are not named playerTeam or enemyTeam. That’s because – at least in Riflestorm – the teams are decided at run time, which I feel is best.
I created an other class called level which stores the player team and enemy team – among other things.
if else
// access like so
if
That’s all, feel free to modify the code to suit your needs.
Download source code:
Comments
No comments yet. Be the first.