05-30-2024, 04:28 PM
(05-30-2024, 10:52 AM)1micha.elok Wrote:(05-29-2024, 06:46 PM)Marcus Wrote: Here you go, tutorial step 5, about really simple moving sprites....
Question on Example 5
Each time new tutorial is released, it's more and more interesting. I like it
I would like to run different scenarios of the pumpkins before and after touching the ground.
Before Touching The Ground
click image to zoom in
After Touching The Ground
click image to zoom in
Could you please advice me how to do those scenarios ? I have tried to code, but it's failed.
Code:'----------------
' INITIALIZATION
'----------------
...
'load game assets (pumpkin, map, player)
visible vPumpkinImage1 = loadimage("assets5/pumpkin1.png", 4, 1)
visible vPumpkinImage2 = loadimage("assets5/pumpkin2.png", 4, 1)
..
foreach f in flags
select f.flag
...
case "pumpkin"
o = CreatePumpkinEnemy(f.x, -10, f.z)
EA_AddObject(o)
endsel
next
...
'-----------
' GAME LOOP
'-----------
EA_Run()
'-----------
' FUNCTIONS
'-----------
...
function CreatePumpkinEnemy(x, y, z)
enemy = EA_Object()
'--------------------------------------------------
'It's intended to have different Pumpkin Image
'before touch the ground and after touch the ground
'but it seems failed :(
if enemy.Y()<0
enemy.SetSprite(vPumpkinImage1, 0, true)
enemy.SetHeight(height(vPumpkinImage1)/64)
enemy.SetRadius(0.5*width(vPumpkinImage1)/64)
elseif enemy.Y()>=0
enemy.SetSprite(vPumpkinImage2, 0, true)
enemy.SetHeight(height(vPumpkinImage2)/64)
enemy.SetRadius(0.5*width(vPumpkinImage2)/64)
endif
'--------------------------------------------------
enemy.SetPos(x, y, z)
enemy.SetYaw(rad(rnd(360)))
enemy.cel = 0
enemy.Update = function(dt)
.cel = (.cel + dt*8)%4
.SetCel(int(.cel))
'--------------------------------------------------------------
'It's intended to give different gravity or speed for each enemy
'but it seems failed :(
objs = .SectorObjects()
if objs and sizeof(objs)
for i = 0 to sizeof(objs) - 1
gravity = rnd(0,1)*dt
next
endif
'---------------------------------------------------------------
res = .Move(.DX()*0.75*dt, gravity, .DZ()*0.75*dt, 0)
if res.w 'wall
.SetYaw(rad(deg(atan2(res.dx, res.dz)) - 90 + rnd()*180))
endif
endfunc
return enemy
endfunc
I've made some changes to the code and marked them with Marcus
Code:
'----------------
' INITIALIZATION
'----------------
include "libs/enginea.n7"
set window "test_ex5", 320, 200, false, 3
set redraw off
EA_SetView(primary, rad(90), 0.05, 50)
'load game assets (pumpkin, map, player)
visible vPumpkinImage1 = loadimage("assets5/pumpkin1.png", 4, 1)
visible vPumpkinImage2 = loadimage("assets5/pumpkin2.png", 4, 1)
flags = EA_LoadMap("assets5/map_test5.json")
player = unset
foreach f in flags
select f.flag
case "player"
player = EA_FpsPlayer()
player.SetPos(f.x, f.floorY, f.z)
case "pumpkin"
' Marcus: You might as well use the ceilingY field of the flag. Calling the object
' function Move will push down the enemy to a correct position anyway; you could set it
' to -1000 and the result would be exactly the same.
'o = CreatePumpkinEnemy(f.x, -100, f.z)
o = CreatePumpkinEnemy(f.x, f.ceilingY, f.z)
' EndMarcus
EA_AddObject(o)
endsel
next
EA_AddObject(player)
EA_SetCamera(player)
EA_SetUpdateAction(Update)
'-----------
' GAME LOOP
'-----------
EA_Run()
'-----------
' FUNCTIONS
'-----------
function Update(dt)
if keydown(KEY_ESCAPE, true) EA_Stop()
endfunc
function CreatePumpkinEnemy(x, y, z)
enemy = EA_Object()
'--------------------------------------------------
'It's intended to have different Pumpkin Image
'before touch the ground and after touch the ground
'but it seems failed :(
'if enemy.Y()<0
' enemy.SetSprite(vPumpkinImage1, 0, true)
' enemy.SetHeight(height(vPumpkinImage1)/64)
' enemy.SetRadius(0.5*width(vPumpkinImage1)/64)
'elseif enemy.Y()>=0
' enemy.SetSprite(vPumpkinImage2, 0, true)
' enemy.SetHeight(height(vPumpkinImage2)/64)
' enemy.SetRadius(0.5*width(vPumpkinImage2)/64)
'endif
'--------------------------------------------------
' Marcus: the behavior of this enemy should probably be controlled by a state variable. The
' enemy should behave in one way until it touches the ground, then change state and behave in
' another way.
enemy.state = 0 ' State 0 means it's falling, state 1 will mean it's on the gound.
enemy.SetSprite(vPumpkinImage1, 0, true)
enemy.SetHeight(height(vPumpkinImage1)/64)
enemy.SetRadius(0.5*width(vPumpkinImage1)/64)
' Add a field for vertical speed, random.
enemy.dy = 0.25 + rnd()*1
' EndMarcus
enemy.SetPos(x, y, z)
enemy.SetYaw(rad(rnd(360)))
enemy.cel = 0
enemy.Update = function(dt)
.cel = (.cel + dt*8)%4
.SetCel(int(.cel))
'-------------------------------------------------------
'It's intended to give different gravity for each enemy
'but it seems failed :(
'objs = .SectorObjects()
'if objs and sizeof(objs)
' for i = 0 to sizeof(objs) - 1
' gravity = rnd(0,1)*dt
' next
'endif
'-------------------------------------------------------
' Marcus: do different things depending on the state variable.
if .state = 0 ' Falling from the sky (... ceiling).
' Only move vertically with the speed setup earlier.
res = .Move(0, .dy*dt, 0, 0)
' Check res.g (g as in ground) for collision. It will be true when the enemy collides
' with the ground.
if res.g
' Change state to 1.
.state = 1
' Set "gravity" to 1.
.dy = 1
' Change sprite image. Do the images have the same size? I just assume they do :)
.SetSprite(vPumpkinImage2, 0, true)
endif
else
' Use the dy variable.
res = .Move(.DX()*0.75*dt, .dy, .DZ()*0.75*dt, 0)
if res.w 'wall
.SetYaw(rad(deg(atan2(res.dx, res.dz)) - 90 + rnd()*180))
endif
endif
' EndMarcus
endfunc
return enemy
endfunc