Working in 3d is the easiest way to do this, even if it's a 2d game. I'm making a 2d game with this system.
set display mode 1024,768,32
set window on
sync on
sync rate 0
hide mouse
type POSITION
X as float
Y as float
Z as float
T as integer
endtype
dim PROPOS(1) as POSITION
dim MOUSEPOS(1) as POSITION
dim DELTAPOS(1) as POSITION
dim CURSORPOS(0) as POSITION
CURSORSPEEDFACTOR#=.048 `any number that makes the cursor line up with the mouse pointer.
`set a global depth for the 2d plane the game will run on
z#=30
`cursor object
make object plane 1,1,1
color object 1,rgb(0,0,255)
`projectile object
make object box 2,.2,.2,.5
color object 2,rgb(255,0,0)
`allow for a certain degree of inaccuracy with projectile
make object collision box 2,-.2,-.2,-.25,.2,.2,.25,1
`background object
make object plane 3,10,10
color object 3,rgb(0,255,0)
position object 3,0,5,z#
do
`initialize mouse cursor postion to center
if INICURSOR<TIMER() and CPOS<10
position mouse screen width()/2,screen height()/2
CURSORPOS(0).X=0
CURSORPOS(0).Y=0
INICURSOR=TIMER()+10
inc CPOS
endif
`find mouse position
MOUSEPOS(1).X=mx
MOUSEPOS(1).Y=my
mx=mousex()
my=mousey()
`find difference between position of the plane and position of the mouse
MOUSEPOS(0).X=abs(MOUSEPOS(1).X-mx)
MOUSEPOS(0).Y=abs(MOUSEPOS(1).Y-my)
`modify position of plane accordingly
if MOUSEPOS(1).X>mx
DELTAPOS(1).X=DELTAPOS(1).X-CURSORSPEEDFACTOR#*MOUSEPOS(0).X
endif
if MOUSEPOS(1).X<mx
DELTAPOS(1).X=DELTAPOS(1).X+CURSORSPEEDFACTOR#*MOUSEPOS(0).X
endif
if MOUSEPOS(1).Y>my
DELTAPOS(1).Y=DELTAPOS(1).Y+CURSORSPEEDFACTOR#*MOUSEPOS(0).Y
endif
if MOUSEPOS(1).Y<my
DELTAPOS(1).Y=DELTAPOS(1).Y-CURSORSPEEDFACTOR#*MOUSEPOS(0).Y
endif
`set new cursor position
CURSORPOS(0).X=CURSORPOS(0).X+DELTAPOS(1).X
CURSORPOS(0).Y=CURSORPOS(0).Y+DELTAPOS(1).Y
`reset values
DELTAPOS(1).X=0
DELTAPOS(1).Y=0
`x#,y# are position of camera. z# is depth the cursor will be rendered. A smaller z value will render it on top of the game plane
position object 1,x#+CURSORPOS(0).X,y#+CURSORPOS(0).Y,z#-.25
`setup projectile system
if FIRED=0
position object 2,0,0,5
`save cursor position. Set PROPOS(1).Z to the 2d world depth, not the cursor depth
PROPOS(1).X=object position x(1)
PROPOS(1).Y=object position y(1)
PROPOS(1).Z=z#
`point projectile at cursor
point object 2,PROPOS(1).X,PROPOS(1).Y,PROPOS(1).Z
if mouseclick()=1
FIRED=1
endif
endif
if FIRED=1
`move projectile
if PROJTIMER<TIMER()
move object 2,1
PROJTIMER=TIMER()+10
endif
`set falloff distance for projectile
if object position x(2)>100 or object position y(2)>100 or object position z(2)>100 then FIRED=0
`set collision with specific world object
if object collision(2,0)=3
FIRED=0
inc HIT
endif
endif
position camera 0,0,0
point camera 0,0,100
text 0,0,"FPS: "+str$(screen fps())
text 0,10,"Hit: "+str$(HIT)
sync
loop
This is bare bones and probably not optimized. Add all the bells and whistles you want. Set the object collision to detect within a fixed range of enemy objects, whatever you want.
This version puts the projectile on the same depth as the target.
set display mode 1024,768,32
set window on
sync on
sync rate 0
hide mouse
type POSITION
X as float
Y as float
Z as float
T as integer
endtype
dim PROPOS(1) as POSITION
dim MOUSEPOS(1) as POSITION
dim DELTAPOS(1) as POSITION
dim CURSORPOS(0) as POSITION
CURSORSPEEDFACTOR#=.048 `any number that makes the cursor line up with the mouse pointer.
`set a global depth for the 2d plane the game will run on
z#=30
`cursor object
make object plane 1,1,1
color object 1,rgb(0,0,255)
`projectile object
make object box 2,1,1,5
color object 2,rgb(255,0,0)
`background object
make object box 3,10,10,10
color object 3,rgb(0,255,0)
position object 3,0,5,z#
do
`initialize mouse cursor postion to center
if INICURSOR<TIMER() and CPOS<10
position mouse screen width()/2,screen height()/2
CURSORPOS(0).X=0
CURSORPOS(0).Y=0
INICURSOR=TIMER()+10
inc CPOS
endif
`find mouse position
MOUSEPOS(1).X=mx
MOUSEPOS(1).Y=my
mx=mousex()
my=mousey()
`find difference between position of the plane and position of the mouse
MOUSEPOS(0).X=abs(MOUSEPOS(1).X-mx)
MOUSEPOS(0).Y=abs(MOUSEPOS(1).Y-my)
`modify position of plane accordingly
if MOUSEPOS(1).X>mx
DELTAPOS(1).X=DELTAPOS(1).X-CURSORSPEEDFACTOR#*MOUSEPOS(0).X
endif
if MOUSEPOS(1).X<mx
DELTAPOS(1).X=DELTAPOS(1).X+CURSORSPEEDFACTOR#*MOUSEPOS(0).X
endif
if MOUSEPOS(1).Y>my
DELTAPOS(1).Y=DELTAPOS(1).Y+CURSORSPEEDFACTOR#*MOUSEPOS(0).Y
endif
if MOUSEPOS(1).Y<my
DELTAPOS(1).Y=DELTAPOS(1).Y-CURSORSPEEDFACTOR#*MOUSEPOS(0).Y
endif
`set new cursor position
CURSORPOS(0).X=CURSORPOS(0).X+DELTAPOS(1).X
CURSORPOS(0).Y=CURSORPOS(0).Y+DELTAPOS(1).Y
`reset values
DELTAPOS(1).X=0
DELTAPOS(1).Y=0
`x#,y# are position of camera. z# is depth the cursor will be rendered. A smaller z value will render it on top of the game plane
position object 1,x#+CURSORPOS(0).X,y#+CURSORPOS(0).Y,z#-.25
`setup projectile system
if FIRED=0
position object 2,0,-20,z#
`save cursor position. Set PROPOS(1).Z to the 2d world depth, not the cursor depth
PROPOS(1).X=object position x(1)
PROPOS(1).Y=object position y(1)
PROPOS(1).Z=z#
`point projectile at cursor
point object 2,PROPOS(1).X,PROPOS(1).Y,PROPOS(1).Z
if mouseclick()=1
FIRED=1
endif
endif
if FIRED=1
`move projectile
if PROJTIMER<TIMER()
move object 2,1
PROJTIMER=TIMER()+10
endif
`set falloff distance for projectile
if object position x(2)>100 or object position y(2)>100 or object position z(2)>100 then FIRED=0
`set collision with specific world object
if object collision(2,0)=3
FIRED=0
inc HIT
endif
endif
position camera 0,0,0
point camera 0,0,100
text 0,0,"FPS: "+str$(screen fps())
text 0,10,"Hit: "+str$(HIT)
sync
loop