Okay... I've been working on this for a few weeks now (part time, of course). It's a bit (perhaps a little more than a bit
) random, but I thought i'd post it to share some of the simple AI code that I use in the classes.
Storyline: You're an undead ghost called Zebedy and you try to invade the world, between chasing random people in the park to suck their souls.
Levels: Currently, I've only done the first level. It's set in a park with trees, a statue, and a simple matrix terrain. The graphics are quite simple because i'm relying on whatever I can find suitable that was packaged with DB products I have got.
AI: Basic by most standards. Civillians walk around randomly and run when you come within range, emitting a screaming sound. (it sounds pretty funny)
Soldiers patrol randomly, and shoot you when you come within range. (a gunshot sound is played)
Movement: Movement is fully implemented with a simple 'space bar' attack, that hurls an energy ball at the target.
Effects:
Blood - not yet implemented, but attempted
Yea, it's probably not much compared to most of the stuff on this board, but I thought someone might be interested.
Screenshots:
Main Menu:
Attacking:
Helpless men running:
Getting shot by soldiers:
Code:
part of man.cpp
// overloaded AI function
void man::Handle(player* argP)
{
if ( alive ) // make sure it isn't dead
{
// change the angle to face the waypoint
if ( !runningAway )
{
YAngle = dbAtanFull(destX - X, destZ - Z);
}
else
{
// ensure that angle will not lead to movement off the map edge
if ( dbNewXValue ( X,YAngle, speed ) > 9500 && dbNewXValue ( X, YAngle, speed ) < 500 )
{
if ( dbNewZValue ( Z, YAngle, speed ) > 9500 && dbNewZValue ( Z, YAngle, speed ) < 500 )
{
// on current course player will go out of bounds, run somewhere random instead
destX = 500 + dbRnd ( 9000 );
destZ = 500 + dbRnd ( 9000 );
YAngle = dbAtanFull ( destX - X, destZ - Z);
}
}
}
// if zebedy comes within range, the man is running away from him
if ( calcdist ( argP->getX(), argP->getZ(), X, Z ) < LOS )
{
// play scream - not that this will save him from what is to come
if ( !dbSoundPlaying ( 3 ) && !screamTriggered )
{
// angle the object to run in the opposite direction from zebedy
YAngle = dbWrapValue ( 180.0f + dbAtanFull(argP->getX() - X, argP->getZ() - Z) );
// play screaming sound
dbPlaySound ( 3 );
}
moving = true;
runningAway = true;
screamTriggered = true;
}
else
{
screamTriggered = false; // allow player to scream again
runningAway = false; // resume previous tasks
}
if ( moving )
{
// move at different speeds, depending on if the runningAway variable == true
if ( runningAway )
{
// move the enemy towards the position at double speed
changeposition ( dbNewXValue ( X, YAngle, speed*2 ), 0.0f, dbNewZValue ( Z, YAngle, speed*2 ) );
}
else
{
// move the enemy towards the position
changeposition ( dbNewXValue ( X, YAngle, speed ), 0.0f, dbNewZValue ( Z, YAngle, speed ) );
}
updateposition ( ); // update position
dbLoopObject ( objID, moveStart, moveEnd ); // loop animation sequence
}
// set to idle if destination is reached
if ( calcdist ( X, Z, destX, destZ ) < 50.0f )
{
moving = false;
dbLoopObject ( objID, idleStart, idleEnd );
}
dbYRotateObject ( objID, YAngle ); // rotate the man
}
return;
}