Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement simple tick-based engine in c++?

Tags:

c++

mud

I'm writing a text game and I need a simple combat system, like in MUDs, you issue commands, and once in a while "tick" happens, when all those commands execute, player and monsters deal damage, all kinds of different stuff happens. How do I implement that concept? I thought about making a variable that holds last tick time, and a function that just puts events on stack and when that time is (time +x) executes them all simutaniously. Is there any easier or cleaner variant to do that?

What would be possible syntax for that?

double lastTickTime;
double currentTime;

void eventsPile(int event, int target)
{
// how do i implement stack of events? And send them to execute() when time is up?
}

void execute(int event, int target)
{
     if ((currentTime - lastTickTime) == 2)
     {
         eventsHandler(event, target);
     }    
     else 
     { // How do I put events on stack?
     }
}
like image 234
Dvole Avatar asked Nov 20 '10 19:11

Dvole


1 Answers

The problem with simple action stack is that the order of actions will probably be time based - whoever types fastest will strike a first hit. You should probably introduce priorities in the stack, so that for instance all global events trigger first, then creatures' action events, but those action events are ordered by some attribute like agility, or level. If a creature has higher agility then that it gets the first hit.

like image 171
Dialecticus Avatar answered Oct 18 '22 05:10

Dialecticus