Hello Quell Dieu,
it would be far more easy, if you could provide your current progress state (code),
so it would be easier for us to help you.
Let me drop the first stone into the river:
The first step to a good enemy AI is the correct structure of the enemy creation and handling.
The following code demonstrates a basic enemy creation method.
/*
Project: enemyAI
Created: 17.03.2017
AI TUTORIAL #1
BASIC AI ARRAY and TYPES
This code does provide a simple Enemy Array Setup. The code will be the base
for a simple state-machine type AI.
The first definitions are very important in order to get a good start-
*/
// AGK SETUP
// =========
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "enemyAI" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
// TYPES
// =====
// Create a type for a 3d waypoint
type waypoint_def
px# as float
py# as float
pz# as float
endtype
// Create a Type for the Enemy
type Game_enemy_def
obj as integer // object number
lifetxt as integer // text number
name$ as string // a name
action as integer // the current action from the AI, aka State-machine
life# as float // amount of life
waypoints as waypoint_def[] // create an array of waypoints inside the enemy array
endtype
// ARRAYS
// ======
Dim Game_Enemy[] as Game_enemy_def // create the emtpy array
// GLOBALS
// =======
// GAME INITIALIZATION
// ===================
create_level()
firstenemy = Add_Game_Enemy("martin", "RedBox", 0.0, 0.0, 0.0, 100.0)
// MAIN LOOP
// =========
do
Handle_Game_Enemies()
Print( ScreenFPS() )
Sync()
loop
// FUNCTIONS
// =========
// This functions creates an enemy and register it inside the Enemy array
function Add_Game_Enemy(name$, EnemyType$, px#, py#, pz#, life#)
// grow the size of the array and get the last position
Game_Enemy.length = Game_Enemy.length + 1
arrpos = Game_Enemy.length
// create different enemy types, in this chase a red box
if EnemyType$ = "RedBox"
obj = CreateObjectBox(1,1,1)
SetObjectColor(obj,255,0,0,255)
// here you can also add some specific and unique variables to the array
endif
// create a text, that will float over the enemy and display name and life
lifetxt = CreateText("") // can be empty
SetTextSize(lifetxt,20)
SetTextPosition(lifetxt,-1000,-1000)
SetTextAlignment(lifetxt,1)
// update all common variables inside the array
Game_Enemy[arrpos].obj = obj
Game_Enemy[arrpos].lifetxt = lifetxt
Game_Enemy[arrpos].name$ = name$
Game_Enemy[arrpos].life# = life#
Game_Enemy[arrpos].action = 0
endfunction arrpos // return enemy array position
// delete an enemy from the array, aswell as the 3d model of the enemy
function Remove_Game_Enemy(arrpos)
DeleteObject(Game_Enemy[arrpos].obj)
Game_Enemy.remove(arrpos)
endfunction
// this function walks though all of the enemies and updates them
function Handle_Game_Enemies()
// loop though all registerd enemies to handle them
for arrpos = 0 to Game_Enemy.length
obj = Game_Enemy[arrpos].obj
// =>
// here comes a state- machine based on the .action variable inside the enemy array type
// <=
// get the current position of the object
x# = GetObjectX(obj)
y# = GetObjectY(obj)+(GetObjectSizeMaxY(obj)-GetObjectSizeMinY(obj))*2 // about the double y size of the object
z# = GetObjectZ(obj)
// update enemy Name and life
lifetxt = Game_Enemy[arrpos].lifetxt
SetTextString(lifetxt, Game_Enemy[arrpos].name$+chr(10)+str(Game_Enemy[arrpos].life#) )
SetTextPosition(lifetxt,GetScreenXFrom3D(x#,y#,z#),GetScreenYFrom3D(x#,y#,z#))
next arrpos
endfunction
// this function just creates somekind of a level
function create_level()
plane = CreateObjectPlane(100,100) // create a plane
RotateObjectLocalX(plane,90) // turn it, so it lays on the ground
SetObjectColor(plane,0,100,0,255) // set the color to dark green
endfunction