Testing: Path finding module by Rich

magic

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Mar 5, 2018
I'm testing path finding module by @Rich

649


This is a good module and fast. The module post is here : https://www.cerberus-x.com/community/threads/path-finding-class.787/#post-6214

The code for the above testing game is here:
Testing Paths module by @Rich:
Import mojo
Import paths
Function Main:Int();New myClass;Return 0;End
Class myClass Extends App
    Field p:Path
    Field map:Int[]
    Const gameGridX:Int=50
    Const gameGridY:Int=50
    Field hero:Int
    Field enermy:Int[]
    Const enermyAmount:Int=10
    Field target:Int=-1
    Field moveTimer:Int
    Const MOVE_SPEED:Int=100
    Method OnCreate:Int()
        p = New Path()
        'resize map
        map = map.Resize(gameGridX*gameGridY)
        'put item into map
        For Local x:Int=0 Until gameGridX
            For Local y:Int=0 Until gameGridY
                If Rnd(10)<8
                    map[x+(y*gameGridX)]=0 '0=nothing
                Else
                    map[x+(y*gameGridX)]=1 '1=tree
                endif
            Next
        next
        'put hero on map
        Local newPlace:Int
        Repeat
            newPlace=Int(Rnd(gameGridX*gameGridY))
        Until map[newPlace]=0
         hero=newPlace
        'put enermy on map
        enermy=enermy.Resize(enermyAmount)
        For Local k:Int=0 Until enermyAmount
            Local newPlace:Int
            Repeat
                newPlace=Int(Rnd(gameGridX*gameGridY))
            Until map[newPlace]=0
            enermy[k]=newPlace
        next
        'reset moving timer
        moveTimer=Millisecs()
       Return 0
    End
    Method OnUpdate:Int()
        If MouseHit(0)
            target=Int(Int(MouseX()/8)+(Int(MouseY()/6)*gameGridX))
            If map[target]<>0 Then
                target=-1 'cannot go
            Else
                p.GetRoute(hero,target,map,gameGridX,gameGridY, 0, true)
            endif
        endif
        If Millisecs()-moveTimer>MOVE_SPEED
            moveTimer=Millisecs()
            'move the hero
            If target<>-1   
                p.GetRoute(hero,target,map,gameGridX,gameGridY, 0, true)
                If p.Route.Length()>1 Then
                    hero=p.Route[p.Route.Length()-2]
                endif
            endif
            'move the enermy
            For Local k:Int=0 Until enermyAmount
                If Rnd(10)<4 'make less effective
                    p.GetRoute(enermy[k],hero,map,gameGridX,gameGridY, 0, true)
                    If p.Route.Length()>1 Then
                        enermy[k]=p.Route[p.Route.Length()-2]
                    endif
                endif
            Next
        endif
        Return 0
    End
    
    Method OnRender:Int()
        Cls 0,0,0
        'render the map
        For Local x:Int=0 Until gameGridX
            For Local y:Int=0 Until gameGridY
                Select map[x+(y*gameGridX)]
                Case 0
                    'nothing
                    SetColor(0,255,125)
                    DrawPoint((x*8)+4,(y*6)+3)
                Case 1
                    'tree
                    SetColor(0,255,0)
                    DrawCircle((x*8)+4,(y*6)+3,4)
                end
            Next
        next
        'render hero
        SetColor(255,255,255)
        Local x:Int=hero Mod gameGridX
        Local y:Int=hero / gameGridX
        DrawCircle((x*8)+4,(y*6)+3,4)
        DrawText("hero",(x*8)+10,(y*6)+10)
        'render target
        If target<>-1
            SetColor(255,255,255)
            Local x:Int=target Mod gameGridX
            Local y:Int=target / gameGridX
            DrawRect((x*8)+4,(y*6)-3,1,12)
            DrawRect((x*8)-4,(y*6)+3,16,1)
        endif
        'render enermy
        For Local k:Int=0 Until enermyAmount
            SetColor(255,0,0)
            Local x:Int=enermy[k] Mod gameGridX
            Local y:Int=enermy[k] / gameGridX
            DrawCircle((x*8)+4,(y*6)+3,4)
        next
        'general info
        SetColor(255,255,255)
        DrawText("Click to move a hero",0,0)
        Return 0
    End
End
 

rickychus

Member
Joined
Sep 26, 2017
Hello,
I have executed the example that was shared. My doubt, this is how it should be seen? The example does not stretch and occupies the entire screen. Maybe it's my system's error?
Thanks for sharing!
R.-

PS: If I set the hero outside the grid, the example stops with an error.

650
 
Last edited:

magic

Active Member
3rd Party Module Dev
3rd Party Tool Dev
Joined
Mar 5, 2018
Nothing wrong with your system.

Actually, the test code didn't factor that issue :p (because there is no one screen size in CerberusX :cool: )

The solution; Probably try increase the grid to 100x100.

gameGridX=100
gameGridY=100
 

rickychus

Member
Joined
Sep 26, 2017
Haa! Okay, I just wanted to be sure. Thanks for the example.
I hope to have some time to study CerberusX soon.
All the examples that I find will help me a lot.
R.-
 
Top Bottom