How to implement a simple tick-based engine in C ++?

I am writing a text game, and I need a simple combat system, like in MUD, you issue commands, and from time to time there is a β€œtick” when all these commands are executed, the player and monsters do damage, all kinds of different kinds happen. How to implement this concept? I was thinking of creating a variable that contains the last time ticks, and a function that simply pushes events on the stack, and when that time (time + x) executes them all syntactically. Is there a simpler or cleaner option?

What could be the syntax for this?

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? } } 
+6
c ++ mud
source share
3 answers

The problem with a simple action stack is that the order of actions is likely to be based on time β€” the one who will be the fastest will strike first. You should probably push priorities onto the stack, so that, for example, all global events are fired first, and then events of creature events, but these action events are ordered using some attribute, such as maneuverability or level. If the creature has a higher maneuverability, then he gets the first blow.

+1
source share

From what I saw, most of these engines are events, not times. with a new tick caused by some interval after the end of the last tick . (thus mainly avoiding the tick question taking more time)

It also simplifies implementation; you just have a game loop that fires the tick event and then sleep s / yield for the required interval. Which is trivial.

This can be simplified by modeling the world as a tree, where each element controls propagation events (such as ticks ) to its children. as long as you avoid / control the loops, this works well (I did it).

This effectively reduces the tick system to something like this (psudocode):

 while (isRunning) { world->tick(); sleep(interval); } 

In most cases, they need little to become much more attractive than adjusting the length of the previous duration.

Any actions by individual objects will be part of their own action queue and processed during their own tick events.

Usually, user commands will be divided into "ingame" and "meta" commands, any game will simply change their character actions, which will be processed in the next tick, as usual for any other object.

A simple round battle follows naturally from this foundation. in real time it can be modeled with finer tick division with an additional β€œtime pool”.

+1
source share

Use a timer that runs every x ms (while x is your tick time), follow any actions pushed on the stack in this method.

-one
source share

All Articles