I have a hard time deciding how I should pre allocate game objects. Its easy if I use the C style approach of having the same struct for every object and just allocate an array of it for each layer of objects. But I find it harder to do this in OOP (C#) with lots of different (derived) classes. Do you create a seperate array for every kind of object (making a generic array class) ? Or is it sensible to make an array class that reserves an array of the base class where I can add sprites of different types and then call a GetObject method that returns an object by a given type and makes a downcast by the caller:
class ObjectArray{ public Object[] objects; public int capacity; public int size; public ObjectArray(int capacity) { this.capacity = capacity; this.size = 0; this.objects = new Object[capacity]; } public void AddObject(Object spr) { if (size == capacity) return; objects[size++] = spr; } public Object GetObject(byte type) { for (int i = 0; i < size; i++) { if (objects.type == type) return objects; } return null; }}void pseudoMethod(){ AnEnemy enm = (AnEnemy)enemyObjects.GetObject(ObjectTypes.AN_ENEMY); enm.state = ObjectStates.ALIVE; enm.CustomInitMethod();}