I’m working on an android game and have been considering my main game loop. Should the NPC class in my android java game have it’s own move function, or should the move function be moved into the game class? Is there any advantage between option one and option two, CPU load, size of compiled apk, maintainability etc.
I wanted to know if either of these two different code examples I just wrote up if one was the “correct” way.
SUDO CODE: Option One – The NPC class has it’s own move function
class Game {
Npc npcs[] = new Npc[10];
public Game(){
while(running){
for(loop=0;loop<npcs.count(); loop++){
npcs[loop].move();
}
draw();
}}}
SUDO CODE: Option Two – The move function is part of the Game class
class Game {
Npc npcs[] = new Npc[10];
protected moveNPC(){
for(loop=0;loop<npcs.count();loop++){
npcs[loop].x = // change x value
npcs[loop].y = // change y value
}
}
public Game(){
while(running){
moveNPC();
draw();
}}}
There will hardly be any difference in terms of efficiency or hardware use, so you should go with the Option One.
NPCis a class that represents a group of objects that all have their behavior encapsulated inmove()method – this is a basic OOP thing. Means your Option Two approach will work, but it’s wrong from the Object Oriented point of view. Hope this helps.