Thursday, July 28, 2016

Monkey-X - Dungeon/Cave Generator Rectangles - code example


Import mojo

Class tilemap    
    Field tilew:Int
    Field tileh:Int
    Field width:Int
    Field height:Int
    Field map:Int[][]
    Method New(width:Int,height:Int,tilew:Int,tileh:Int)
        Self.width=width
        Self.height=height
        Self.tilew=tilew
        Self.tileh=tileh
        map = New Int[width][]
        For Local i=0 Until width
            map[i] = New Int[height]
        Next
        makemap
        settiles
    End Method
    ' Here we create the walls
    Method settiles()
        'create walls
        For Local y=1 Until height-1
        For Local x=1 Until width-1
            Local t:Int=map[x][y]
            If t=0 And map[x+1][y] = 1 Then map[x][y] = 2
            If t=0 And map[x][y+1] = 1 Then map[x][y] = 2
            If t=0 And map[x-1][y] = 1 Then map[x][y] = 2
            If t=0 And map[x][y-1] = 1 Then map[x][y] = 2
        Next
        Next
        ' get corners
        For Local y=1 Until height-1
        For Local x=1 Until width-1
            Local t:Int=map[x][y]
            If t=0 And map[x+1][y] = 2 And map[x][y+1] = 2 Then map[x][y] = 3
            If t=0 And map[x-1][y] = 2 And map[x][y+1] = 2 Then map[x][y] = 3
            If t=0 And map[x][y-1] = 2 And map[x+1][y] = 2 Then map[x][y] = 3
            If t=0 And map[x-1][y] = 2 And map[x][y-1] = 2 Then map[x][y] = 3
        Next
        Next
        'set corners to value 2
        For Local y=1 Until height-1
        For Local x=1 Until width-1
            If map[x][y] = 3 Then map[x][y] = 2
        Next
        Next
    End Method
    Method makemap()
        'here we make random rectangles and see if they 
        'are placed next to another rectangle.
        'This will grow a random level...
        Local w1:Int=Rnd(1,4)
        Local h1:Int=Rnd(1,4)
        Local x1:Int=width/2
        Local y1:Int=height/2
        
        drawmaprect(x1,y1,w1,h1)
    
    
        Local maxrooms:Int=(width*height)/30
        Local numrooms:Int=0
        Local cnt:Int=0
        While numrooms < maxrooms And cnt<15000
            cnt+=1
            Local done:Bool=False
            w1=Rnd(1,4)
            h1=Rnd(1,4)
            x1=Rnd(3,width-5)
            y1=Rnd(3,height-5)
            'check right
            If map[x1][y1] = 1 And map[x1+1][y1] = 0 And map[x1+1][y1-2] = 0 And map[x1+1][y1+2] = 0 
                drawmaprect(x1,y1,w1,h1)
                numrooms+=1
                done=True
            End If
            'check bottom
            If done=False And map[x1][y1] = 1 And map[x1][y1+1] = 0 And map[x1-2][y1+1] = 0 And map[x1+2][y1+1] = 0
                drawmaprect(x1,y1,w1,h1)
                numrooms+=1
                done=True
            End If
            'check top
            If done=False And y1+h1<height-1
                If map[x1][y1+h1+1] = 1 And map[x1][y1+h1] = 0 And map[x1-2][y1+h1] = 0  And map[x1+2][y1+h1] = 0
                    drawmaprect(x1,y1,w1,h1)
                    numrooms+=1
                    done=True
                End If
            End If
            'check left
            If done=False And x1+w1<width-1
                If map[x1+w1+1][y1] = 1 And map[x1+w1][y1] = 0 And map[x1+w1][y1-2] = 0 And map[x1+w1][y1+2] = 0
                    drawmaprect(x1,y1,w1,h1)
                    numrooms+=1                    
                End If
            End If
        Wend

    End Method
    Method drawmaprect(x:Int,y:Int,w:Int,h:Int)
        For Local y1=y To y+h
        For Local x1=x To x+w
            If x1>-1 And y1>-1 And x1<width And y1<height
                map[x1][y1] = 1
            End If
        Next
        Next
    End Method
    Method draw()
        SetColor 255,255,255
        For Local y=0 Until height
        For Local x=0 Until width
            Local t:Int=map[x][y]
            Select t
                Case 1 'wall
                SetColor 100,100,100
                DrawRect x*tilew,y*tileh,tilew,tileh
                Case 2 'wall
                SetColor 200,200,200
                DrawRect x*tilew,y*tileh,tilew,tileh                
            End Select            
        Next
        Next
    End Method
End Class

Global m:tilemap

Class MyGame Extends App

    Method OnCreate()
        Seed = GetDate[5]
        SetUpdateRate(60)
        createrandommap()
    End Method
    Method OnUpdate()  
        If KeyHit(KEY_SPACE) Or MouseHit(MOUSE_LEFT)
            createrandommap()
        End If      
    End Method
    Method OnRender()
        Cls 0,0,0 
        m.draw
        SetColor 255,255,255
        DrawText "Press Space or Tab to create new level",0,0
    End Method
End Class

Function createrandommap()
    Local r:Int=Rnd(0,3)
    Local w:Int,h:Int,tw:Int,th:Int
    Select r
    Case 0 
        w=20;h=15;tw=32;th=32
    Case 1 
        w=20*2;h=15*2;tw=16;th=16
    Case 2 
        w=20*4;h=15*4;tw=8;th=8
    End Select
    m = New tilemap(w,h,tw,th)
End Function

Function Main()
    New MyGame()
End Function

Wednesday, July 20, 2016

Monkey-X - Grey Rnd Poly grid backgrounds - code example


Import mojo

Class gridbackground
    Field mapwidth:Int
    Field mapheight:Int
    Field tilewidth:Float
    Field tileheight:Float
    Field mapdepth:Int
    Field mapx:Float[][]
    Field mapy:Float[][]
    Method New(mapwidth:Int,mapheight:Int)
        Self.mapwidth = mapwidth
        Self.mapheight = mapheight
        tilewidth = DeviceWidth()/Float(mapwidth)
        tileheight = DeviceHeight()/Float(mapheight)
        mapx = New Float[mapwidth][]
        mapy = New Float[mapwidth][]
        For Local i = 0 Until mapwidth
            mapx[i] = New Float[mapheight]
            mapy[i] = New float[mapheight]
        Next 
        makegrid    
    End Method
    Method makegrid()
        For Local y=0 Until mapheight
        For Local x=0 Until mapwidth
            mapx[x][y] = x*tilewidth + Rnd(-tilewidth/3,tilewidth/3)
            mapy[x][y] = y*tileheight + Rnd(-tileheight/3,tileheight/3)
        Next
        Next
    End Method
    Method draw()
        For Local y=0 Until mapheight-1
        For Local x=0 Until mapwidth-1
            Local pol:Float[8]
            pol[0] = mapx[x][y]
            pol[1] = mapy[x][y]
            pol[2] = mapx[x+1][y]
            pol[3] = mapy[x+1][y]
            pol[4] = mapx[x+1][y+1]
            pol[5] = mapy[x+1][y+1]
            pol[6] = mapx[x][y+1]
            pol[7] = mapy[x][y+1]
            Local a:Int=Rnd(0,256)
            SetColor a,a,a
            DrawPoly pol
        Next
        Next    
    End Method
End Class

Global mygbg:gridbackground

Class MyGame Extends App
    
    Method OnCreate()
        SetUpdateRate(1)
        mygbg = New gridbackground(25,25)
    End Method
    Method OnUpdate() 
        mygbg = New gridbackground(Rnd(10,30),Rnd(10,30))       
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        mygbg.draw
    End Method
End Class


Function Main()
    New MyGame()
End Function

Tuesday, July 19, 2016

Monkey-X - Beginners - Sorting IntLists - code example


Import mojo

Class MyGame Extends App

    Field mylist1:List<Int> = New IntList
    Field mylist2:List<Int> = New IntList
    
    Method OnCreate()
        SetUpdateRate(60)
        mylist1.AddLast(10)
        mylist1.AddLast(50)
        mylist1.AddLast(1)                
        mylist1.AddLast(31)
        mylist1.AddLast(93)
        mylist1.AddLast(12)
        'sort low>hight
        mylist1.Sort()
        'list 2
        mylist2.AddLast(10)
        mylist2.AddLast(50)
        mylist2.AddLast(1)                
        mylist2.AddLast(31)
        mylist2.AddLast(93)
        mylist2.AddLast(12)
        'sort high>low
        mylist2.Sort(False)

    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawText "Sorting Lists",0,0
        Local cnt:Int=0
        For Local i:=Eachin mylist1
            DrawText i,10,cnt*10+50
            cnt+=1
        Next
        cnt=0
        For Local i:=Eachin mylist2
            DrawText i,200,cnt*10+50
            cnt+=1
        Next
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Sorting StringLists - code example


Import mojo

Class MyGame Extends App

    Field mylist1:List<String> = New StringList
    Field mylist2:List<String> = New StringList
    
    Method OnCreate()
        SetUpdateRate(60)
        mylist1.AddLast("hello")
        mylist1.AddLast("this")
        mylist1.AddLast("is")                
        mylist1.AddLast("a")
        mylist1.AddLast("test")
        mylist1.AddLast("mkay")
        'sort alphabetically
        mylist1.Sort()
        'list 2
        mylist2.AddLast("hello")
        mylist2.AddLast("this")
        mylist2.AddLast("is")                
        mylist2.AddLast("a")
        mylist2.AddLast("test")
        mylist2.AddLast("mkay")
        'sort reversed alhabetically
        mylist2.Sort(False)

    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawText "Sorting Lists",0,0
        Local cnt:Int=0
        For Local i:=Eachin mylist1
            DrawText i,10,cnt*10+50
            cnt+=1
        Next
        cnt=0
        For Local i:=Eachin mylist2
            DrawText i,200,cnt*10+50
            cnt+=1
        Next
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monday, July 18, 2016

Monkey-X - Beginners - SetScissor - code example


Import mojo

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawText "SetScissor Example (x,y,w,h)",10,10
        ' SetScissor x,y,w,h
        SetScissor(50,50,640-100,480-100)
        ' Here we draw a image on the entire screen
        ' and that will only be drawn inside the 
        ' scissor area.
        SetColor 50,20,0
        Local cx:Float=320
           Local cy:Float=240
        For Local angle=0 Until 360-10 Step 32
            Local x1:Float=cx+Cos(angle)*640
            Local y1:Float=cy+Sin(angle)*480
            Local x2:Float=cx+Cos(angle+10)*640
            Local y2:Float=cy+Sin(angle+10)*480
            DrawPoly([cx,cy,x1,y1,x2,y2])
        Next
        
    End Method
End Class


Function Main()
    New MyGame()
End function

Sunday, July 17, 2016

Monkey-X - Point in Triangle 2D - code example


Import mojo

Class MyGame Extends App

    ' set up the triangle
    Field x1:Float=100
    Field y1:Float=100
    Field x2:Float=100
    Field y2:Float=200
    Field x3:Float=200
    Field y3:Float=100
    ' collision info
    Field col:String
    
    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate()        
        If pointintriangle2d(MouseX(),MouseY(),x1,y1,x2,y2,x3,y3)
            col="Collision"
        Else
            col="No Collision"
        End If
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawPoly([x1,y1,x2,y2,x3,y3])
        DrawText col,0,0
    End Method
End Class

' by RaGR (Ralph G. Roeske). from blitzbasic.com
Function pointintriangle2d(px:Float,pz:Float, x1:Float,y1:Float,x2:Float,y2:Float,x3:Float,y3:Float) 
    Local bc:Float,ca:Float,ab:Float,ap:Float,bp:Float,cp:Float,abc:Float
    bc = x2*y3 - y2*x3 
    ca = x3*y1 - y3*x1 
    ab = x1*y2 - y1*x2
    ap = x1*pz - y1*px
    bp = x2*pz - y2*px
    cp = x3*pz - y3*px
    abc = Sgn(bc + ca + ab)
    If (abc*(bc-bp+cp)>=0) And (abc*(ca-cp+ap)>=0) And (abc*(ab-ap+bp)>=0) Return True
End Function

Function Main()
    New MyGame()
End Function