Handling multiple game modes with one game controller [on hold]











up vote
-1
down vote

favorite












I have a basic GameController with a finite state machine to handle game logic.



I'm adding game modes and coming across problems cleanly implementing them. The core functionality of the game stays the same--Physics, input handling, level creation--but I have many places where I need an if/else block because functionality is slightly different. At two game modes it was manageable but not the nicest implementation. I'm adding an extra game mode and want to clean it up.



Here's some simplified example code:



void OnBlocksCleared(int count) {
score += getScoreFromClearedBlocks(count);
scoreController.setScore(score);

if (currentMode == GameMode.Endless) {
DataManager.Instance.AddTotalBlocksCleared(count);
} else if (currentMode == GameMode.Rush) {
// Do nothing
} else if (currentMode == GameMode.TimeAttack) {
if (allBlocks.Count <= 0) {
currentState = GameState.Win;
}
}
}


There are methods that have much more logic and only a single line if/else for a specific game mode (if TimeAttack time is up, if Rush level is completed, etc), and some methods like above where the if/else blocks take a lot of space. I was thinking about implementing an interface for each game mode but it wasn't easy to cleanly implement. Here is what I was planning.



public interface IGameBehavior {
void SetHighscore();
void OnBlocksCleared(int count);
void SetRemainingTime(float remainingTime);
}


Then I would have a EndlessBehavior, TimeAttackBehavior, RushBehavior and at the bottom of corresponding methods call the specific method like so.



void OnBlocksCleared(int count) {
// Do everything that's shared

gameBehavior.OnBlocksCleared(count);
}


This doesn't seem to work well because if the behavior needs to modify specifics of the gameController it will become tightly coupled. Also in many cases only one or two of the game behaviors actually do anything.










share|improve this question







New contributor




GameDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Ludisposed, t3chb0t, Toby Speight, Vogel612 yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Ludisposed, t3chb0t, Toby Speight, Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.













  • Please don't post simplified code but the real one.
    – t3chb0t
    yesterday










  • Are we supposed to post 300 lines of irrelevant code? Thanks for the downvotes.
    – GameDev
    yesterday










  • If you think they are irrelevant and don't wish a Code Review then asking on Software Engineering might be a better idea. Since this is Code Review and there is no code in your question, the question is off-topic.
    – t3chb0t
    yesterday










  • Okay I apologize. This looks like the wrong forum indeed. I'm in the middle of large refactoring and thought this would be a good place to clean up my architecture but I suppose I was wrong.
    – GameDev
    yesterday















up vote
-1
down vote

favorite












I have a basic GameController with a finite state machine to handle game logic.



I'm adding game modes and coming across problems cleanly implementing them. The core functionality of the game stays the same--Physics, input handling, level creation--but I have many places where I need an if/else block because functionality is slightly different. At two game modes it was manageable but not the nicest implementation. I'm adding an extra game mode and want to clean it up.



Here's some simplified example code:



void OnBlocksCleared(int count) {
score += getScoreFromClearedBlocks(count);
scoreController.setScore(score);

if (currentMode == GameMode.Endless) {
DataManager.Instance.AddTotalBlocksCleared(count);
} else if (currentMode == GameMode.Rush) {
// Do nothing
} else if (currentMode == GameMode.TimeAttack) {
if (allBlocks.Count <= 0) {
currentState = GameState.Win;
}
}
}


There are methods that have much more logic and only a single line if/else for a specific game mode (if TimeAttack time is up, if Rush level is completed, etc), and some methods like above where the if/else blocks take a lot of space. I was thinking about implementing an interface for each game mode but it wasn't easy to cleanly implement. Here is what I was planning.



public interface IGameBehavior {
void SetHighscore();
void OnBlocksCleared(int count);
void SetRemainingTime(float remainingTime);
}


Then I would have a EndlessBehavior, TimeAttackBehavior, RushBehavior and at the bottom of corresponding methods call the specific method like so.



void OnBlocksCleared(int count) {
// Do everything that's shared

gameBehavior.OnBlocksCleared(count);
}


This doesn't seem to work well because if the behavior needs to modify specifics of the gameController it will become tightly coupled. Also in many cases only one or two of the game behaviors actually do anything.










share|improve this question







New contributor




GameDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as off-topic by Ludisposed, t3chb0t, Toby Speight, Vogel612 yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Ludisposed, t3chb0t, Toby Speight, Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.













  • Please don't post simplified code but the real one.
    – t3chb0t
    yesterday










  • Are we supposed to post 300 lines of irrelevant code? Thanks for the downvotes.
    – GameDev
    yesterday










  • If you think they are irrelevant and don't wish a Code Review then asking on Software Engineering might be a better idea. Since this is Code Review and there is no code in your question, the question is off-topic.
    – t3chb0t
    yesterday










  • Okay I apologize. This looks like the wrong forum indeed. I'm in the middle of large refactoring and thought this would be a good place to clean up my architecture but I suppose I was wrong.
    – GameDev
    yesterday













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a basic GameController with a finite state machine to handle game logic.



I'm adding game modes and coming across problems cleanly implementing them. The core functionality of the game stays the same--Physics, input handling, level creation--but I have many places where I need an if/else block because functionality is slightly different. At two game modes it was manageable but not the nicest implementation. I'm adding an extra game mode and want to clean it up.



Here's some simplified example code:



void OnBlocksCleared(int count) {
score += getScoreFromClearedBlocks(count);
scoreController.setScore(score);

if (currentMode == GameMode.Endless) {
DataManager.Instance.AddTotalBlocksCleared(count);
} else if (currentMode == GameMode.Rush) {
// Do nothing
} else if (currentMode == GameMode.TimeAttack) {
if (allBlocks.Count <= 0) {
currentState = GameState.Win;
}
}
}


There are methods that have much more logic and only a single line if/else for a specific game mode (if TimeAttack time is up, if Rush level is completed, etc), and some methods like above where the if/else blocks take a lot of space. I was thinking about implementing an interface for each game mode but it wasn't easy to cleanly implement. Here is what I was planning.



public interface IGameBehavior {
void SetHighscore();
void OnBlocksCleared(int count);
void SetRemainingTime(float remainingTime);
}


Then I would have a EndlessBehavior, TimeAttackBehavior, RushBehavior and at the bottom of corresponding methods call the specific method like so.



void OnBlocksCleared(int count) {
// Do everything that's shared

gameBehavior.OnBlocksCleared(count);
}


This doesn't seem to work well because if the behavior needs to modify specifics of the gameController it will become tightly coupled. Also in many cases only one or two of the game behaviors actually do anything.










share|improve this question







New contributor




GameDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have a basic GameController with a finite state machine to handle game logic.



I'm adding game modes and coming across problems cleanly implementing them. The core functionality of the game stays the same--Physics, input handling, level creation--but I have many places where I need an if/else block because functionality is slightly different. At two game modes it was manageable but not the nicest implementation. I'm adding an extra game mode and want to clean it up.



Here's some simplified example code:



void OnBlocksCleared(int count) {
score += getScoreFromClearedBlocks(count);
scoreController.setScore(score);

if (currentMode == GameMode.Endless) {
DataManager.Instance.AddTotalBlocksCleared(count);
} else if (currentMode == GameMode.Rush) {
// Do nothing
} else if (currentMode == GameMode.TimeAttack) {
if (allBlocks.Count <= 0) {
currentState = GameState.Win;
}
}
}


There are methods that have much more logic and only a single line if/else for a specific game mode (if TimeAttack time is up, if Rush level is completed, etc), and some methods like above where the if/else blocks take a lot of space. I was thinking about implementing an interface for each game mode but it wasn't easy to cleanly implement. Here is what I was planning.



public interface IGameBehavior {
void SetHighscore();
void OnBlocksCleared(int count);
void SetRemainingTime(float remainingTime);
}


Then I would have a EndlessBehavior, TimeAttackBehavior, RushBehavior and at the bottom of corresponding methods call the specific method like so.



void OnBlocksCleared(int count) {
// Do everything that's shared

gameBehavior.OnBlocksCleared(count);
}


This doesn't seem to work well because if the behavior needs to modify specifics of the gameController it will become tightly coupled. Also in many cases only one or two of the game behaviors actually do anything.







c# game interface unity3d






share|improve this question







New contributor




GameDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




GameDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




GameDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









GameDev

1




1




New contributor




GameDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





GameDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






GameDev is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as off-topic by Ludisposed, t3chb0t, Toby Speight, Vogel612 yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Ludisposed, t3chb0t, Toby Speight, Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by Ludisposed, t3chb0t, Toby Speight, Vogel612 yesterday


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Ludisposed, t3chb0t, Toby Speight, Vogel612

If this question can be reworded to fit the rules in the help center, please edit the question.












  • Please don't post simplified code but the real one.
    – t3chb0t
    yesterday










  • Are we supposed to post 300 lines of irrelevant code? Thanks for the downvotes.
    – GameDev
    yesterday










  • If you think they are irrelevant and don't wish a Code Review then asking on Software Engineering might be a better idea. Since this is Code Review and there is no code in your question, the question is off-topic.
    – t3chb0t
    yesterday










  • Okay I apologize. This looks like the wrong forum indeed. I'm in the middle of large refactoring and thought this would be a good place to clean up my architecture but I suppose I was wrong.
    – GameDev
    yesterday


















  • Please don't post simplified code but the real one.
    – t3chb0t
    yesterday










  • Are we supposed to post 300 lines of irrelevant code? Thanks for the downvotes.
    – GameDev
    yesterday










  • If you think they are irrelevant and don't wish a Code Review then asking on Software Engineering might be a better idea. Since this is Code Review and there is no code in your question, the question is off-topic.
    – t3chb0t
    yesterday










  • Okay I apologize. This looks like the wrong forum indeed. I'm in the middle of large refactoring and thought this would be a good place to clean up my architecture but I suppose I was wrong.
    – GameDev
    yesterday
















Please don't post simplified code but the real one.
– t3chb0t
yesterday




Please don't post simplified code but the real one.
– t3chb0t
yesterday












Are we supposed to post 300 lines of irrelevant code? Thanks for the downvotes.
– GameDev
yesterday




Are we supposed to post 300 lines of irrelevant code? Thanks for the downvotes.
– GameDev
yesterday












If you think they are irrelevant and don't wish a Code Review then asking on Software Engineering might be a better idea. Since this is Code Review and there is no code in your question, the question is off-topic.
– t3chb0t
yesterday




If you think they are irrelevant and don't wish a Code Review then asking on Software Engineering might be a better idea. Since this is Code Review and there is no code in your question, the question is off-topic.
– t3chb0t
yesterday












Okay I apologize. This looks like the wrong forum indeed. I'm in the middle of large refactoring and thought this would be a good place to clean up my architecture but I suppose I was wrong.
– GameDev
yesterday




Okay I apologize. This looks like the wrong forum indeed. I'm in the middle of large refactoring and thought this would be a good place to clean up my architecture but I suppose I was wrong.
– GameDev
yesterday















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Ellipse (mathématiques)

Quarter-circle Tiles

Mont Emei