Hey Guys,

i'm still alive and helping out 3run one or the other time tongue

This time, i've built a small concept for creating synchronized events in games like platformers or similar.

The idea behind it is that we have some kind of global counter that runs in loops:


Now we can define some events that will happen at a certain angle and react to them. If we want to offset events to each other, we just have to adjust the events



Code:
Click to reveal..
Code:
#include <acknex.h>
#include <default.c>

var game_loop = 0;
var game_scale = 2.0; // A bit faster is better :)

// Adjust these to change the timing behaviour
var event_start_raise = 30;
var event_stop_raise = 50;
var event_start_sink = 90;
var event_stop_sink = 110;

function lerp(var x, var y, var z)
{
	return x * (1.0 - z) + y * z;
}

// delta:    angle offset
// velocity: local time scaling
// x:        X-Offset
function synchronized_obj(var delta, var velocity, var x)
{
	while(1)
	{
		// Create our local "time frame"
		// the higher we scale the game_loop
		// the faster our object will change its
		// states.
		var time = cycle(velocity * game_loop - delta, 0, 360);
	
		var y;
		if(time >= event_stop_sink) {
			// After events (sunken)
			y = 400;
		}
		else if(time >= event_start_sink) {
			// Third time slice (sinking)
			y = lerp(350, 400, (time - event_start_sink) / (event_stop_sink - event_start_sink));
		}
		else if(time >= event_stop_raise) {
			// Second time slice (up)
			y = 350;
		}
		else if(time >= event_start_raise) {
			// First time slice (raising)
			y = lerp(400, 350, (time - event_start_raise) / (event_stop_raise - event_start_raise));
		}
		else {
			// Before events (sunken)
			y = 400;
		}
	
		draw_quad(
			NULL,
			vector(x - 50, y, 0),
			NULL,
			vector(100, 50, 0),
			NULL,
			COLOR_RED,
			100,
			0);
	
		wait(1);
	}
	
}

VECTOR * vang(var i)
{
	return vector(100 + 75 * sinv(i), 100 - 75*cosv(i), 0);
}

function debug_the_loop()
{
	while(1)
	{
		// Circle
		var i = 0;
		draw_line(
			vang(0),
			NULL,
			100);
		for(i = 0; i <= 360; i += 5)
		{
			draw_line(
				vang(i),
				COLOR_GREEN,
				100);
		}
		
		draw_line(
			vector(100, 100, 0),
			NULL,
			100);
		draw_line(
			vector(100, 100, 0),
			COLOR_RED,
			100);
		draw_line(
			vang(event_start_raise),
			COLOR_RED,
			100);
			
		draw_line(
			vector(100, 100, 0),
			NULL,
			100);
		draw_line(
			vector(100, 100, 0),
			COLOR_RED,
			100);
		draw_line(
			vang(event_stop_raise),
			COLOR_RED,
			100);
			
		draw_line(
			vector(100, 100, 0),
			NULL,
			100);
		draw_line(
			vector(100, 100, 0),
			COLOR_RED,
			100);
		draw_line(
			vang(event_start_sink),
			COLOR_RED,
			100);
			
		draw_line(
			vector(100, 100, 0),
			NULL,
			100);
		draw_line(
			vector(100, 100, 0),
			COLOR_RED,
			100);
		draw_line(
			vang(event_stop_sink),
			COLOR_RED,
			100);
			
		draw_line(
			vector(100, 100, 0),
			NULL,
			100);
		draw_line(
			vector(100, 100, 0),
			COLOR_BLUE,
			100);
		draw_line(
			vang(ang(game_loop)),
			COLOR_BLUE,
			100);
		
		wait(1);
	}
}

function main()
{
	wait(2);
	
	debug_the_loop();
	
	// Change the first param to modify synchronization
	// the second to increase the "local looping speed"
	synchronized_obj(0,   1.0, 100);
	synchronized_obj(80,  1.0, 200);
	synchronized_obj(160, 1.0, 300);
	
	synchronized_obj(0,   2.0, 500);
	synchronized_obj(80,  2.0, 600);
	synchronized_obj(160, 2.0, 700);
	while(1)
	{
		// Well, that sucks here, but
		// it does the job!
		// would be better to do iterative 
		// incrementing of game_loop to
		// allow dynamic game_scale without
		// changing the total state of the game
		game_loop = game_scale * total_ticks;
		
		draw_line(
			vector(000, 400, 0),
			NULL,
			100);
		draw_line(
			vector(000, 400, 0),
			COLOR_WHITE,
			100);
		draw_line(
			vector(800, 400, 0),
			COLOR_WHITE,
			100);
			
		DEBUG_VAR(screen_size.x, 0);
		DEBUG_VAR(screen_size.y, 16);
		
		wait(1);
	}
}



As usual, i'd like to have credits for this, but take it and make awesome stuff with it! NOW!

Regards
Felix


Visit my site: www.masterq32.de