Hi guys,
this is my first code snippet, and i`m very proud to announce my peace of code.What it does it`s self explanatory.It makes certain event to apply to severeal objects at the same time without interupting of each other.The first version was too buggy, i was a bit lazy and the result was not what i was aiming at.I make an effort, put myself in shape and everything goes like i imagine it.The code was completly rewriten, but work as a miracle now.Enjoy!
The code is showing few things:
It shows how to cast the event on many objects and in the same time every object does what is needed of it without interupts of others(first version of my code does the oposite, second object doing the same was interupting the first stoping it in space before reaching the point where it`s needed to reach).In this snippet there are several plains which at mouseclick change their position and rotation at the same time.
There you can also learn about mouse toggle, it`s good trick to learn.I learn it hard way(like the cave man learn about fire).This was causing problems at first because without mouse toggle objects was launching to their target when they was picked by mouse with click + left button hold, one after another.
There is some UDT`s and array manipulation also.
`Some basic setup
sync on : sync rate 60
backdrop off
make camera 1
position camera 1,300,0,-1000
`Making UDT.ID is for identifycation and DEAD for state check if object can be picked or skipped.
type enemy
ID as integer
DEAD as boolean
endtype
`Define of max_objects as constant, so we are sure it`s not gonna change.
#CONSTANT max_objects = 10
`Making of our object array
DIM object(max_objects) as enemy
`Using finite loop to assign our objects DEAD state as false.
For x = 1 to max_objects
object(x).DEAD = 0
next x
`Main Loop
do
`It`s allways good to know how the program behave on exeuting certain stuff.
text 0,0,"fps: "+str$(screen fps())
`Finite loop go trough all objects and create, position, scale and assign them ID if they do not exist.
For id = 1 to max_objects
if object exist(id)=0
make object plain id,10,10
position object id,id*40,0,0
scale object id,300,300,10
object(id).ID = id
endif
next id
`Finite loop go trough all objects and check it`s state, if it`s dead(shooted) it makes it spin and go away.
For x = 1 to max_objects
if object(x).DEAD = 1
z=z+20
rotate object x,0,0,wrapvalue(z)
pos_z = object position z(x)
pos_z = pos_z + 20
position object x,object position x(x),object position y(x),pos_z
endif
next x
`Finite loop go trough all objects and check it`s state and position in space, if it`s too far and state DEAD, it`s deleted.
for x = 1 to max_objects
if object(x).DEAD = 1 and object position z(x)>3000
delete object x
object(x).DEAD = 0
endif
next x
`Check if mouse is clicked, if not toggle_mouse = 0
if mouseclick()=0
toggle_mouse = 0
endif
`Check toggle_mouse state and if 0, pick an object at mouse position.
if toggle_mouse = 0
selected_object = pick object (mousex(),mousey(),1,max_objects)
endif
`If mouseclick is present and selected_object exists, toggle_mouse change state to prevent
`draging and left mouse button hold to pick other objects.
if mouseclick() = 1 and selected_object > 0
toggle_mouse = 1
object(selected_object).DEAD = 1
endif
`If mouseclick is present and selected_object exists not, toggle_mouse change state to prevent
`left button hold + hover on object to trigger the event while mouseclick were present on empty space.
if mouseclick()=1 and selected_object = 0
toggle_mouse = 1
endif
`Basic camera control for testing purposes.
control camera using arrowkeys 1,1,1
sync
loop
Feedback appreciated!
EDIT: Comments added.
Where there is a will, there is a way.
I often edit my posts, that`s who i am
