Hello, I'd like to ask how I would make AI able to move around dynamic objects, In my game my player is static, I need to move the entire world around me.
I have tried obstacles, re-positioning when the player moves, I use the "AI Entity Go to position" command towards my player constantly.
Whenever I move up and down, left and right I have to reset the AI, an dstop the Call of AI Entity Go to position which stops the automatic moving around obstacles so now they can move through walls instead of going around them.
I need to be able to run away from the AI and be able to move the world around me including all objects the AI must go around these objects even when moving.
How would I solve the issue?
Here's what I got so far:
// Setup
AI Start :AI Set Radius 5
Sync On : Sync Rate 60
Backdrop Off : Autocam Off
// UDT
// used for Player and Enemies
Type Graphics
id as Integer
xpos as Integer
ypos as Integer
angle as float
width as Integer
height as Integer
EndType
// Used for Vertex positions
Type Obstacles
p1 as Integer
p2 as Integer
p3 as Integer
p4 as Integer
EndType
// Globals
Global TotalEnemies = 1
Global UsingKeys = 0
Global EnemiesIDrange = 500
Global PlayerIsMoving = 0
// Wall Setup
Global Wall as Obstacles
Wall.p1 = 5
Wall.p2 = 310
Wall.p3 = 500
Wall.p4 = 330
// Player Setup
Global Player as Graphics
Player.id = 300
Player.xpos = Screen Width() / 2
Player.ypos = Screen Height() / 2
Load Image "player.png", Player.id
Sprite Player.id, Player.xpos, Player.ypos, Player.id
Player.width = Sprite Width(Player.id) / 2
Player.height = Sprite Height(Player.id) / 2
// center sprite
Offset Sprite Player.id, Player.width, Player.height
// Enemies Setup
Dim Enemies(TotalEnemies) as Graphics
For x = 0 to TotalEnemies
Enemies(x).id = EnemiesIDrange
Enemies(x).xpos = RND(400)
Enemies(x).ypos = RND(300)
// DARK AI setup
AI Add Enemy Enemies(x).id, 0
AI Set Entity Speed Enemies(x).id, 70
AI Set Entity Position Enemies(x).id, Enemies(x).xpos, Enemies(x).ypos
AI Set Entity Idle Position Enemies(x).id, Enemies(x).xpos, Enemies(x).ypos
// Increase the ID of individual enemies
Inc EnemiesIDrange
Next x
// Create Obstacles
MakeLevel()
// Draw Obstacles on screen
DrawLevel()
///////////////////////
////// Main Loop //////
///////////////////////
Do
Cls
// Render our Graphics
RenderWorld()
// UserInput
InputWorld()
// AI is moving to position of player
UpdateWorld()
AI Update
Sync
Loop
Function UpdateWorld()
For x = 0 to TotalEnemies
// If I haven't pressed either of the arrow keys (movement) then move the AI towards me (Im static)
If PlayerIsMoving = 0
Enemies(x).xpos = Int( AI Get Entity X(Enemies(x).id) )
Enemies(x).ypos = Int( AI Get Entity Z(Enemies(x).id) )
AI Entity Go To Position Enemies(x).id, Screen Width() / 2, Screen Height() / 2
Else
// If I am moving, then move the AI away from me (player appears to move away now gives dynamic feel)
If PlayerIsMoving = 1
// reset to stop "AI go to position"
AI Entity Reset Enemies(x).id
AI Set Entity Position Enemies(x).id, Enemies(x).xpos, Enemies(x).ypos
Endif
EndIf
Next x
Endfunction
Function RenderWorld()
// Player is always in the middle of the screen
Sprite Player.id, Player.xpos, Player.ypos, Player.id
// Draw the level - walls
DrawLevel()
// Render AI
DrawEntity()
Endfunction
Function MakeLevel()
AI Start New Obstacle 1
AI Add Obstacle Vertex Wall.p1, Wall.p2
AI Add Obstacle Vertex Wall.p1, Wall.p4
AI Add Obstacle Vertex Wall.p3, Wall.p4
AI Add Obstacle Vertex Wall.p3, Wall.p2
AI End New Obstacle 0, 1
AI Complete Obstacles
Endfunction
Function DrawLevel()
Line Wall.p1, Wall.p4, Wall.p1, Wall.p2
Line Wall.p1, Wall.p2, Wall.p3, Wall.p2
Line Wall.p3, Wall.p2, Wall.p3, Wall.p4
Line Wall.p3, Wall.p4, Wall.p1, Wall.p4
Ink RGB(255, 255, 255), 0
Endfunction
Function DrawEntity()
// draw all the enemies
For x = 0 to TotalEnemies
lookDirX = Enemies(x).xpos + int( sin(angle#) * 20.0 )
lookDirY = Enemies(x).ypos + int( cos(angle#) * 20.0 )
Circle Enemies(x).xpos, Enemies(x).ypos, 10
Line Enemies(x).xpos, Enemies(x).ypos, lookDirX, lookDirY
Next x
Endfunction
Function InputWorld()
// used so we can turn off some AI commands in: UpdateWorld() function
If UsingKeys = 1
PlayerIsMoving = 1
Else
PlayerIsMoving = 0
EndIf
// For movement, I move my AI away from me
// I destroy all my Obstacles, change their position and create them in their new locations
// upkey
If Keystate(200)
For x = 0 to TotalEnemies
Text 300, 150, "up is pressed"
// move AI away
Enemies(x).ypos = Enemies(x).ypos + 1
// remove so we can add
AI Remove Obstacle 1
// change location
Wall.p2 = Wall.p2 + 1
Wall.p4 = Wall.p4 + 1
// create new located Obstacles
MakeLevel()
Next x
Endif
//
//
// downkey
If Keystate(208)
For x = 0 to TotalEnemies
Text 300, 150, "down is pressed"
Enemies(x).ypos = Enemies(x).ypos - 1
AI Remove Obstacle 1
Wall.p2 = Wall.p2 - 1
Wall.p4 = Wall.p4 - 1
MakeLevel()
Next x
EndIf
// leftkey
If Keystate(203)
For x = 0 to TotalEnemies
Text 300, 150, "left is pressed"
Enemies(x).xpos = Enemies(x).xpos + 1
AI Remove Obstacle 1
Wall.p3 = wall.p3 + 1
Wall.p1 = Wall.p1 + 1
MakeLevel()
Next x
EndIf
// rightkey
If Keystate(205)
For x = 0 to TotalEnemies
Text 300, 150, "right is pressed"
Enemies(x).xpos = Enemies(x).xpos - 1
AI Remove Obstacle 1
Wall.p3 = wall.p3 - 1
Wall.p1 = Wall.p1 - 1
MakeLevel()
Next x
EndIf
// If I press any of the arrow keys, I'm using the keys
If keystate(200) or Keystate(203) or Keystate(208) or Keystate(205)
UsingKeys = 1
Else
UsingKeys = 0
Endif
Text 300, 300, "UsingKeys: " + Str$(UsingKeys) + " PlayerIsMoving: " + Str$(PlayerIsMoving)
Endfunction
help would be greatly appreciated