a roguelike game converted from cpp to FB

Game development specific discussions.
dodicat
Posts: 6930
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: a roguelike game converted from cpp to FB

Postby dodicat » Apr 10, 2021 15:56

You could do an arrayinsert and arraydelete to add or annihilate monsters.

Code: Select all



#macro arrayinsert(a,index,insert)
    If index>=Lbound(a) And index<=Ubound(a)+1 Then
        Var index2=index-Lbound(a)
        Redim Preserve a(Lbound(a) To  Ubound(a)+1)
        For x As long= Ubound(a) To Lbound(a)+index2+1 Step -1
            Swap a(x),a(x-1)
        Next x
        a(Lbound(a)+index2)=insert
    End If
#endmacro

#macro arraydelete(a,index)
    If index>=Lbound(a) And index<=Ubound(a) Then
        For x As long=index To Ubound(a)-1
            a(x)=a(x+1)
        Next x
        Redim Preserve a(Lbound(a) To Ubound(a)-1)
    End If
#endmacro

type monster
    as long headsize
    as long taillength
    as long bodythickness
    as string name
    as long teethsize(1 to 20)
    as single averagetoothsize
    declare constructor
    declare sub show
    static index as long
end type
dim monster.index as long


constructor monster
index+=1
headsize=rnd*50
taillength=headsize+rnd*50
bodythickness=3*headsize
name="Monster "+str(index)
dim as single a
for n as long=1 to 20
    teethsize(n)=rnd*headsize/10
    a+=teethsize(n)
next n
averagetoothsize=a/20
end constructor

sub monster.show
    print "Headsize","taillength","bodythickness","name","average tooth size"
    print headsize,taillength,bodythickness,name,averagetoothsize
end sub

redim as monster m(1 to 3)
for n as long=1 to 3
    m(n).show
next
print:print

dim as monster temp
monster.index-=1

arrayinsert(m,ubound(m)+1,temp)
print "push one at back"
for n as long=lbound(m) to ubound(m)
    m(n).show
next
print:print
print "pop one at index 3"
arraydelete(m,3)
for n as long=lbound(m) to ubound(m)
    m(n).show
next
print:print


temp.constructor
monster.index-=1

print "push new one at index 2"
arrayinsert(m,2,temp)
for n as long=lbound(m) to ubound(m)
    m(n).show
next
print:print
sleep


 
ron77
Posts: 194
Joined: Feb 21, 2019 19:24
Location: Israel
Contact:

Re: a roguelike game converted from cpp to FB

Postby ron77 » Apr 10, 2021 16:36

hello dudicat...

nice... you gave the macro for arrayinsert... however what i need is actually to delete/remove from the array... :-/ i'm trying to figure out how to modify the macro but don't seem to understand how... you wouldn't have the arraydelete/arrayremove macro code hidden somewhere by chance?
dodicat
Posts: 6930
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: a roguelike game converted from cpp to FB

Postby dodicat » Apr 10, 2021 17:36

Hi ron77
The arraydelete is there already.

Return to “Game Dev”

Who is online

Users browsing this forum: Majestic-12 [Bot] and 2 guests