Game Development Community

Enemy Spawning

by Larry Jennings II · in General Discussion · 04/11/2011 (10:16 am) · 1 replies

Okay, in my game, i want to be able to pre-define enemies, and then be able to respawn enemies of those types later on in it when needed.

I'm curious of what everyones opinions on the best way to do this are.

Is it a good idea to like, create them on a scene somewhere with all their scripting, collision, and behaviors set; save reference to them in an global object under a given name ( like "gruntA" ) , then disable and hide them. Then when needed just get the reference from the global object and copy it for a new enemy?

Links or info would be greatly appreciated, Thanks.

#1
04/14/2011 (9:57 am)
Hello Larry,

You can create any object dynamically (i believe) directly in the script. You can use a behavior applied to an empty sceneobject to delimit the area where you want the enemies (or anything else) appear.

Creating a datablock to the enemy and set up a class to manage events, you can do everything with great practicality.

I am using behavior like this in the project i'm working on now:

//------------------------------------------------------------------
// Controla o spawn dos inimigos no level
//------------------------------------------------------------------

if ( !isObject( EnemySpawnBehavior ) )
{
    %template = new BehaviorTemplate( EnemySpawnBehavior );
    
    %template.friendlyName = "Enemy Spawn Benavior";
    %template.behaviorType = "Enemy";
    %template.description  = "Control the spawns";
    
    %template.addBehaviorField(startTime, "Start time (in seconds)", INT, 0);
    %template.addBehaviorField(count, "Count of enemies", INT, 0);
    %template.addBehaviorField(delay, "Delay between the spawns (in ms)", INT, 100);
    %template.addBehaviorField(enemyType, "Enemy type", ENUM, "", "RedDemonnOrcnOrangeDemonnBigRedDemonnCerberus");
}

/// start the process
function EnemySpawnBehavior::onAddToScene( %this, %scenegraph )
{
	%this.run();
}

/// no use
function EnemySpawnBehavior::onBehaviorAdd( %this )
{
}

function EnemySpawnBehavior::beforeSpawn( %this )
{
	// randomiza um local para o spawn do inimigo
	%position = (%this.Owner.Position.x + ( (%this.Owner.Size.x/2) - getRandom(%this.Owner.Size.x))) SPC (%this.Owner.Position.y + ( (%this.Owner.Size.y/2) - getRandom(%this.Owner.Size.y)));

	// play a effect
	%particle = new t2dParticleEffect() { 
		scenegraph = SceneWindow2D.getScenegraph(); 
	};

	%particle.loadEffect("~/data/particles/smaller_explosion.eff");
	%particle.Position = %position;
	%particle.setLayer(4);
	%particle.setCollisionActive(false, false);

	%particle.playEffect(true);
	
	// shcedule to one seconde (change this based on yor wffect time)
	%this.schedule(1000, spawn, %position);
}

function EnemySpawnBehavior::spawn( %this, %position )
{
	// if has no more enemies to spawn, quit
	if (%this.spawnCount <= 0) return;

	// i use the switch to set the enemy, but is possible to use the config datablock in the enum
	switch$ (%this.enemyType)
	{
		case "RedDemon":
			%mob = new t2dAnimatedSprite() {
				config = NPCRedDemon;
			};
		case "Orc":
			%mob = new t2dAnimatedSprite() {
				config = NPCOrc;
			};
		case "OrangeDemon":
			%mob = new t2dAnimatedSprite() {
				config = NPCOrangeDemon;
			};
		case "BigRedDemon":
			%mob = new t2dAnimatedSprite() {
				config = NPCBigRedDemon;
			};
		case "Cerberus":
			%mob = new t2dAnimatedSprite() {
				config = NPCCerberus;
			};
	}

	%mob.setPosition(%position);

	%mob.addToScene( SceneWindow2D.getScenegraph() );

	%this.schedule(%this.delay, beforeSpawn);

	// remove one from the amount of enemies
	%this.spawnCount--;
}


function EnemySpawnBehavior::run( %this )
{
	// separa as quantidades em uma variavel diferente
	%this.spawnCount = %this.count;

	%this.schedule(%this.startTime*1000, beforeSpawn);
}


Then you can create datablocks like this:

new t2dSceneObjectDatablock (NPCRedDemon)
{
	baseSpeed = 25.0;
	hitPoints = 1;
	score = 10;

	class = "EnemyClass";
	size = "20.640 21.120";
	CollisionCircleScale = "0.25";
	CollisionDetectionMode = "CIRCLE";
	CollisionActiveSend = "1";
	CollisionActiveReceive = "1";
	CollisionCallback = "1";
};

With this, the enemy is dynamically created at some random point within the established area of sceneobject you created and added the behavior.


**bad english