To shoot projectiles, first you have to define a projectile. Do that with a UDT. (I am in linux so i cannot show any playbasic code). Your projectile type should have an x# and y# position, an xVel# and yVel# (velocity, speed) and a lifetime#, i.e. when the projectile will die if it does not hit anything.
Then, define an array to hold all the projectiles that are on screen, and a variable which will hold the next index free. This variable starts at 0. I usually call it numProjectiles.
When you press the left mouse button, you create a projectile at the index location of numProjectiles (which starts at 0 for the first projectile), and increment numProjectiles.
In your main loop, you loop through all your projectiles and do the following:
- Move position according to velocity
- Count down life variable
- If it reaches 0, delete this projectile. This is done by swapping the values of this projectile with the values of the projectile at index numProjectiles-1 (the last projectile), and then setting numProjectiles = numProjectiles - 1.
- Check if projectile is colliding with any other game object
- If so, do some damage on that game object, and delete this projectile in the same way as above
- If the projectile was not deleted then finally we can draw it. The drawing could also be in a separate loop so to not mix up rendering and game logic, but that is up to you
I think that should be it. Creating a projectile would be something like the code you have above:
GunX#=100
GunY#=GetScreenheight()-100
dim projectiles( 400 ) as ProjectileType ; You have to define ProjectileType UDT
numProjectiles = 0
Do
cls rgb(0,20,30)
mx=mousex()
my=mousey()
angle#=getangle2D(gunx#,guny#,mx,my)
Currentangle#=curveangle(angle#,currentangle#,20)
if leftmousebutton() ; or whatever the function is alled
projectiles( numProjectiles ).x# = GunX#
projectiles( numProjectiles ).y# = GunY#
projectiles( numProjectiles ).lifetime# = some range here
projectiles( numProjectiles ).xVel# = cosnewvalue( 0, currentangle#, PROJECTILE_SPEED )
projectiles( numProjectiles ).yVel# = sinnewvalue( 0, currentangle#, PROJECTILE_SPEED )
numProjectiles = numProjectiles + 1
mouseflush()
endif
for i = 0 to numProjectiles - 1
; move projectiles
next
for i = 0 to numProjectiles - 1
circle projectiles(i).x#, projectiles(i).y#, 2, false
next
x2#=cosnewvalue(gunx#,currentangle#,20)
y2#=sinnewvalue(guny#,currentangle#,20)
line gunx#, guny#, x2#, y2#
circle gunx#,guny#,15, true
Sync
loop
hope it helps… Sorry i haven't read through your game idea yet. I read RPG in the title and, well, i am not much of an rpg fan ;) But i will read through it, i promise! Most people are rpg fans though, so i am surprised noone replied yet.
EDIT: Fixed link to types.