SETTING UP YOUR PACKAGE
Renaming Package and Files
All of your code should live inside the package called myTeam.
Please rename your package, Player, and Unit classes in Eclipse.
Right Click --> Refactor --> Rename
Make sure you check the box to rename subpackages
Update the file path for your image in MyTeamPlayer to reflect your new package name
PLAYER CLASS
The Constructor
The constructor is called before the game begins.
public MyTeam(int team, Game g)
{
super(team, g);
// Your team name and image. The image should be in a 4:3 ratio.
setName("MyTeam");
setTeamImage("src/teams/student/myTeam/myTeam.png");
// You can set your team colors here as RGB values.
setColorPrimary(150, 150, 150);
setColorSecondary(255, 255, 255);
setColorAccent(255, 255, 255);
}
Strategy Method
This method is called every frame. It is used to help set your team's strategy and build order.
There are many ways to decide which units to build. The example team simply tries to keep units at a certain ratio to each other. You may also consider...
Time passed in the game
Your team's relative strength to the opponent
The types of components your opponent favors
public void strategy()
{
// In this example our player is ONLY setting a build order
// However, you may want to determine when to attack or defend here too!
// Checks what percentage of the fleet are each type
// This approach scales by unit cost, not absolute numbers.
if(getFleetValuePercentage(Worker.class) < .5f)
{
buildUnit(new Worker(this));
}
else
{
buildUnit(new Fighter(this));
}
}
When you run the program, you'll see Fleet values at the bottom.
Value is the total cost of all your units added up
Count is the number of units you have total.
Resources are listed twice
Store is the number of resources you have available that are unspent
Total is the total amount you have collected over the course of the game.
Draw Method
This method is called every frame when it is enabled. It can be toggled in:
Settings (found in the engine package)
By pressing "q" or "e" during gameplay for the left and right players respectively.
public void draw(Graphics g)
{
// You can use ANY drawing method you would like in here
// This is very useful for showing what your units are trying to do
}
UNIT SUPERCLASS
Action Method
This method is called every frame. During a unit's action, it can:
Turn any direction freely
Accelerate (once per action)
Use one or more weapons
public void action()
{
// If you want all your units to perform a default action, add code here
// Reminder: It won't work unless subclasses call super.action() !
}
Draw Method
This method is called every frame when it is enabled. It can be toggled in:
Settings (found in the engine package)
By pressing "q' or "e" during gameplay
public void draw(Graphics g)
{
// You can use ANY drawing method you would like in here
// This is very useful for displaying data and decisions
}
Your Starter Code
In the action method we split up our logic into attacking and movement. Eventually, you'll want to divide this up into even more layers.
public void action()
{
attack(getWeaponOne());
attack(getWeaponTwo());
movement();
}
The attack method will use a weapon as long as the enemy is valid and that weapon slot is actually filled. Using a weapon that isn't currently available due to cooldown has no effect, so it is safe to try and use it every frame.
public void attack(Weapon w)
{
Unit enemy = getNearestEnemy();
if(enemy != null && w != null)
{
w.use(enemy);
}
}
This movement code is a simple skirmishing method. Find the nearest enemy, and if it is out of your range approach it. Otherwise, move in the opposite direction. What are some advantages and disadvantages of this strategy? What other ways could we use to retreat?
public void movement()
{
Unit enemy = getNearestEnemy();
if(enemy != null)
{
if(getDistance(enemy) > getMaxRange())
{
moveTo(enemy);
}
else
{
turnTo(enemy);
turnAround();
move();
}
}
}
BASIC FIGHTER
Design
The design method determines a few factors:
The unit's frame, which describes how large it is: LIGHT, MEDIUM, HEAVY, or ASSAULT
The unit's style, which shows its cosmetic appearance
The unit's weapons and upgrades
public void design()
{
// This unit costs 6 credits
// Medium Frame (2) Weapons (2) + Upgrades (2)
// For more information, see the units page.
setFrame(Frame.MEDIUM);
setStyle(Style.DAGGER);
addWeapon(new MachineGun(this));
addWeapon(new SmallLaser(this));
addUpgrade(new Plating(this));
addUpgrade(new Shield(this));
}
Making Additional Combat Units
This is going to be a lot of your work in the project. Here are a few basic things you can explore:
Make a second combat ship with a more specialized different archetype:
A tank that has a lot of defense and tries to stay close to the opponent
A sniper that has long range and high damage, but fragile if it gets close
A skirmisher that is very quick and tries to avoid damage through dodging and movement.
Think about how 2-3 different unit types might work together to be stronger in combat
How can the Skirmish method be improved? Do units always want to stay at max range
Overriding Methods
At the start, the fighter behaves the same way as any generic unit on your team. In the future, you may want to override the action() method. Or, you might keep action() the same and override specific methods.
For example, the Gods have the following code in action:
public final void action()
{
core();
special();
act();
movement();
end();
}
Even the core method just calls other more specific methods:
public final void core()
{
calculateCanBeAttackedSoon();
calculateNearestEnemyThreat();
calculateTargets();
calculateStrengths();
spreadMovement();
continueRetreat();
continueRally();
antiMissileSystem();
barrier();
}
Nearly all of my units follow this structure. A specialized unit might only override movement(), or maybe even a specific method like spreadMovement(). Your system will likely not need as many layers as mine, but you'll find it can be helpful to structure your code in this way to really take advanage of inheritence and maximize readability.
Basic Worker
Improving Gatherering
Watch your gather in action. When it activates the Collector, it will slow down a bit. Is it slowing down too soon? Too late? Is it just right? Is there a way to improve the collect method/
Right now your worker completely ignores enemies. Should it run away from threats? If so, what is the trigger? When it takes damage? When an enemy is close? What about the ratio of enemies and allies? Seek a balance of self-preservation but not being too skittish.
Improving Mining
Try watching your miner. If you look carefully, you'll notice that it isn't always mining! It bobs in and out of range. This results in some wasted time. How can you modify the harvest method to be better?