External Exam Download Resources Web Applications Games Recycle Bin

simple spawner | gml

objSpawner Create

ticks = 0;
seconds = 0;

objSpawner Step

// ------------ TIME CALCULATIONS:

ticks += 1;
seconds = ticks div room_speed;

var a_second_has_passed = false;
if (ticks mod room_speed == 0) a_second_has_passed = true;

var two_seconds_have_passed = false;
if (seconds mod 2 == 0) two_seconds_have_passed = true;

var five_seconds_have_passed = false;
if (seconds mod 5 == 0) five_seconds_have_passed = true;


// ------------ SET SPAWN LOCATION:

var xx = room_width / 2; // spawn location x
var yy = room_height / 2; // spawn location y


// ------------ SPAWN CALCULATIONS EVERY SECOND ON THE SECOND:

if a_second_has_passed {
    
    // SPAWN UP TO 5 ENEMIES EVERY SECOND THAT PASSES:
    
    if instance_number(objEnemy1) < 5 {
        var enemy = instance_create_layer(xx,yy,"Instances",objEnemy1);
        with(enemy){
            direction = point_direction(x,y,random(room_width),room_height);
            speed = irandom_range(1,2);
        }
    }
    
    // SPAWN UP TO 3 ENEMIES EVERY TWO SECONDS:
    
    if instance_number(objEnemy2) < 3 and two_seconds_have_passed {
        var enemy = instance_create_layer(xx,yy,"Instances",objEnemy2);
        with(enemy){
            direction = irandom_range(90,180);
            speed = irandom_range(3,4);
        }
    }
    
    // SPAWN UP TO 2 ENEMIES EVERY FIVE SECONDS:
    
    if instance_number(objEnemy3) < 2 and five_seconds_have_passed {
        var enemy = instance_create_layer(xx,yy,"Instances",objEnemy3);
        with(enemy){
            direction = point_direction(x,y,0,0);
            speed = irandom_range(5,6);
        }
    }
    
}