Suggest the way to design several classes
I'm building simple application on as3. Kind of starship game. What 开发者_运维知识库I want to do is to create several different star ships. Each one should have different images (different look), different sets of animation (e.g. when it's flying, burning, damaged), different kind of weapon and also different controllers (e.g. one can be managed by user, another one by computer, and I want to be able to reuse same ships for AI controller as well as for users controls).
Each ship is created in the following way:
- Create entity
- Add spatial
- Add renderers
- Add other components.... ...... n. init the ship
So what I am trying to do:
1) Create StarShip superclass, to store HP (as every ship has it), store spatial (same reason)
2) Create inherited class for any other ship... (It will contain renderer - (responsible for display part), weapon, set of animations), etc
What do you think about such way of composition? Maybe it's better to place everything in super class, and then just create instances using long, long, long constructors like:
StarShip(hp:HP, animations:DICT, weapon:Weapon, ....)
Need advice
I think Abstract Factory can solve it
here is a dfart how to use two craft classes based on abstract StarCraft
class StarCraft
{
private HP hp;
private Spatial spatial;
public StarCraft(HP hp,Spatial spatial)
{
// store HP and Spatial for your needs in base class
this.hp = hp;
this.spatial = spatial;
}
// must be implemented in derived classes
public abstract void Init();
}
internal class TurboStarCraft : StarCraft
{
private Render render;
private OtherComponents otherComponents;
public TurboStarCraft(HP hp, Spatial spatial,Render render, OtherComponents otherComponents) : base(hp, spatial)
{
this.render = render;
this.otherComponents = otherComponents;
}
public void Init()
{
// init somehow;
}
}
internal class SuperStarCraft : StarCraft
{
private Render render;
private OtherComponents otherComponents;
public SuperStarCraft(HP hp, Spatial spatial,Render render, OtherComponents otherComponents) : base(hp, spatial)
{
this.render = render;
this.otherComponents = otherComponents;
}
public void Init()
{
// init somehow;
}
}
class StarCraftFacroty
{
public StarCraft Create(const int craftType)
{
if(craftType == SUPERCRAFT)
{
return new SuperStarCraft(hp, spatial, otherCompenents);
}
if(craftType == TURBOCRAFT)
{
return new TurboStarCraft(hp, spatial, otherCompenents);
}
}
}
精彩评论