Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
That 3d thing
#50
(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 Smile

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 Smile

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
Reply


Messages In This Thread
That 3d thing - by Marcus - 04-20-2024, 11:34 PM
RE: That 3d thing - by johnno56 - 04-21-2024, 02:06 AM
RE: That 3d thing - by Marcus - 04-21-2024, 07:58 AM
RE: That 3d thing - by 1micha.elok - 04-21-2024, 09:46 AM
RE: That 3d thing - by johnno56 - 04-21-2024, 10:15 AM
RE: That 3d thing - by Marcus - 04-21-2024, 10:54 AM
RE: That 3d thing - by johnno56 - 04-21-2024, 12:19 PM
RE: That 3d thing - by Marcus - 04-27-2024, 02:32 PM
RE: That 3d thing - by 1micha.elok - 04-28-2024, 12:25 AM
RE: That 3d thing - by Marcus - 04-28-2024, 12:05 PM
RE: That 3d thing - by 1micha.elok - 04-29-2024, 12:34 AM
RE: That 3d thing - by johnno56 - 04-27-2024, 07:02 PM
RE: That 3d thing - by Marcus - 04-29-2024, 04:30 PM
RE: That 3d thing - by johnno56 - 04-29-2024, 07:54 PM
RE: That 3d thing - by 1micha.elok - 04-30-2024, 02:32 AM
RE: That 3d thing - by Marcus - 05-19-2024, 04:18 PM
RE: That 3d thing - by aliensoldier - 05-19-2024, 09:03 PM
RE: That 3d thing - by Marcus - 05-20-2024, 03:32 PM
RE: That 3d thing - by johnno56 - 05-20-2024, 08:02 PM
RE: That 3d thing - by Marcus - 05-23-2024, 06:42 PM
RE: That 3d thing - by johnno56 - 05-23-2024, 07:51 PM
RE: That 3d thing - by Marcus - 05-23-2024, 08:29 PM
RE: That 3d thing - by Marcus - 05-23-2024, 08:38 PM
RE: That 3d thing - by johnno56 - 05-23-2024, 11:37 PM
RE: That 3d thing - by Marcus - 05-24-2024, 05:04 AM
RE: That 3d thing - by johnno56 - 05-24-2024, 08:12 AM
RE: That 3d thing - by Marcus - 05-24-2024, 10:06 AM
RE: That 3d thing - by Marcus - 05-24-2024, 06:15 PM
RE: That 3d thing - by 1micha.elok - 05-25-2024, 05:26 AM
RE: That 3d thing - by johnno56 - 05-24-2024, 07:58 PM
RE: That 3d thing - by Marcus - 05-25-2024, 11:51 AM
RE: That 3d thing - by johnno56 - 05-26-2024, 02:37 AM
RE: That 3d thing - by aliensoldier - 05-26-2024, 03:36 PM
RE: That 3d thing - by Marcus - 05-26-2024, 05:14 PM
RE: That 3d thing - by aliensoldier - 05-26-2024, 08:52 PM
RE: That 3d thing - by Marcus - 05-27-2024, 06:30 PM
RE: That 3d thing - by 1micha.elok - 05-28-2024, 05:02 AM
RE: That 3d thing - by Marcus - 05-28-2024, 05:14 AM
RE: That 3d thing - by johnno56 - 05-27-2024, 07:59 PM
RE: That 3d thing - by Marcus - 05-28-2024, 05:58 PM
RE: That 3d thing - by 1micha.elok - 05-29-2024, 04:55 AM
RE: That 3d thing - by Marcus - 05-29-2024, 05:33 AM
RE: That 3d thing - by 1micha.elok - 05-29-2024, 12:10 PM
RE: That 3d thing - by Marcus - 05-29-2024, 02:05 PM
RE: That 3d thing - by Marcus - 05-29-2024, 05:15 PM
RE: That 3d thing - by Marcus - 05-29-2024, 06:46 PM
RE: That 3d thing - by 1micha.elok - 05-30-2024, 10:52 AM
RE: That 3d thing - by Marcus - 05-30-2024, 04:28 PM
RE: That 3d thing - by johnno56 - 05-29-2024, 08:08 PM
RE: That 3d thing - by Marcus - 05-29-2024, 08:27 PM
RE: That 3d thing - by Marcus - 05-30-2024, 05:55 PM
RE: That 3d thing - by johnno56 - 05-30-2024, 07:43 PM
RE: That 3d thing - by 1micha.elok - 05-31-2024, 02:11 AM
RE: That 3d thing - by johnno56 - 05-31-2024, 08:24 AM
RE: That 3d thing - by Marcus - 05-31-2024, 09:00 PM
RE: That 3d thing - by 1micha.elok - 06-01-2024, 06:56 AM
RE: That 3d thing - by Marcus - 06-01-2024, 08:10 AM
RE: That 3d thing - by 1micha.elok - 06-05-2024, 08:57 AM
RE: That 3d thing - by Marcus - 06-01-2024, 12:05 PM
RE: That 3d thing - by johnno56 - 06-01-2024, 12:34 PM
RE: That 3d thing - by Marcus - 06-01-2024, 01:35 PM
RE: That 3d thing - by 1micha.elok - 06-05-2024, 05:07 AM
RE: That 3d thing - by Marcus - 06-05-2024, 05:40 PM
RE: That 3d thing - by 1micha.elok - 06-06-2024, 12:37 AM
RE: That 3d thing - by 1micha.elok - 06-07-2024, 11:43 AM
RE: That 3d thing - by johnno56 - 06-07-2024, 10:41 PM
RE: That 3d thing - by 1micha.elok - 06-11-2024, 01:59 AM
RE: That 3d thing - by Marcus - 06-15-2024, 08:27 AM
RE: That 3d thing - by 1micha.elok - 06-16-2024, 10:57 AM
RE: That 3d thing - by johnno56 - 06-15-2024, 10:23 AM
RE: That 3d thing - by kevin - 06-15-2024, 01:33 PM
RE: That 3d thing - by Marcus - 06-25-2024, 05:35 PM
RE: That 3d thing - by johnno56 - 06-25-2024, 08:26 PM
RE: That 3d thing - by 1micha.elok - 06-28-2024, 12:12 AM
RE: That 3d thing - by Marcus - 06-30-2024, 12:38 PM
RE: That 3d thing - by 1micha.elok - 07-02-2024, 11:04 AM

Forum Jump:


Users browsing this thread: 22 Guest(s)