[WIP] Space Game

Showcase your completed projects or get feedback about your work-in-progress

[WIP] Space Game

Postby Virox on Thu Oct 01, 2009 8:48 pm

Hey

I'll use this topic to show my progress. Here is the code of my first test.
Please feel free to give any comments. It's my first time using this kind of engine.
I've uploaded a zipfile, inside is the .lua and image used for the script.

My previous programming experiences is limited to simple vb.net forms or aspx pages. :?

Code: Select all
-- -- ------- -- --
-- -- Startup -- --
-- -- ------- -- --

-- Create Window
display:create ( "My Window", 800, 600, 32, true )


-- Load Textures
my_texture = Image ( )
my_texture:load ( "Scripts/triangle.bmp" )


-- Load Fonts
my_font = Font (  )
my_font:load_system ( "Arial", 18 )


-- Create Main Scene
scene = Layer ( )



-- -- -------------- -- --
-- -- Scene Creation -- --
-- -- -------------- -- --

-- Create Triangle
my_sprite = Sprite ( 0, 5 )
my_sprite.canvas:set_source_image ( my_texture )
my_sprite.canvas:paint ( )


-- Add Triangle & Scene
scene:add_child ( my_sprite )
display: add_child ( scene )


-- Set Variables
varX = scene.x
varY = scene.y
stepX = 1
stepY = 1


-- Create Labels
my_text1 = Sprite ( -390, 270 )
my_text2 = Sprite ( -390, 250 )
my_text1.canvas:set_font ( my_font )
my_text2.canvas:set_font ( my_font )
display:add_child ( my_text1 )
display:add_child ( my_text2 )


-- Create Timer
timer = Timer ( )
timer:start ( 10, true )



-- -- ------ -- --
-- -- Events -- --
-- -- ------ -- --

-- Mouse: On Release
mouse.on_release = function ( delta_x, delta_y )

   varX = mouse.xaxis
   varY = mouse.yaxis

end


-- Timer: On Tick
timer.on_tick = function ( timer )


   -- Calculate distance to destination
   current_pos = Point ( scene.x, scene.y )
   dist = current_pos:get_distance ( varX, varY )


   -- If distance is further than 1 step
   if ( dist > 1 ) then


      -- Calculates angle for turning triangle
      pointx = Point ( scene.x, scene.y )
      newheading = pointx:get_direction ( varX, varY )
      newheading = real_degrees ( newheading )
      currentheading = real_degrees ( scene.rotation )
      difference = newheading - currentheading
   
   
      -- changes negative angle
      if ( difference < 0 ) then
         difference = difference + 360      
      end


      -- Turns triangle towards the smallest angle
      if ( difference > 180 ) then
         scene.rotation = scene.rotation + 1
      else
         scene.rotation = scene.rotation - 1
      end


      -- Makes sure the rotation is between 0 and 360 degrees
      if ( scene.rotation < 0 ) then
         scene.rotation = scene.rotation + 360
      end

      if ( scene.rotation >= 360 ) then
         scene.rotation = scene.rotation - 360
      end


      -- calculates X- and Y-axis step
      stepX = math.cos ( math.rad ( real_degrees ( scene.rotation ) ) )
      stepY = math.sin ( math.rad ( real_degrees ( scene.rotation ) ) )

      scene.x = scene.x + stepX
      scene.y = scene.y + stepY


   -- if distance is shorter then 1 step or 0
   else
      
      scene.x = varX
      scene.y = varY

   end

   my_text1.canvas:clear ( )
   my_text2.canvas:clear ( )
   my_text1.canvas:write ( real_degrees ( scene.rotation )  )
   my_text2.canvas:write ( "cos: " .. stepX .. ", sin: " .. stepY )

end



-- -- --------- -- --
-- -- Functions -- --
-- -- --------- -- --

-- Convert Engine degrees to Real degrees
-- endg: engine degrees
-- rldg: real degrees
function real_degrees ( endg )
   
   rldg = 90 - endg
   
   if (rldg < 0 ) then
      rldg = rldg + 360
   end

   return rldg
   
end
Attachments
Scripts.zip
(1.6 KB) Downloaded 78 times
Virox
 
Posts: 7
Joined: Tue Sep 29, 2009 5:27 pm

Re: [WIP] Space Game

Postby Ivan on Fri Oct 02, 2009 5:40 pm

Technically, your script is fine.
But I do have a couple of suggestions.
There's no guarantee that on_tick will be called every 10 ms.
You can use timer:get_delta_ms ( ) to get the actual amount of time elapsed between ticks.
This will help you to acheive frame-independent motion.
Also, take a closer look at Lua and its object-oriented capabilities.
You would need structure in your code if you plan on making something more advanced.
One more thing, variables like current_pos and dist are local variables.
That is, you won't use them outside of the on_tick function.
You should define them as local current_pos and local dist as not to pollute the global environment.
Ivan
Site Admin
 
Posts: 111
Joined: Thu Dec 27, 2007 9:21 pm

Re: [WIP] Space Game

Postby Phil on Fri Oct 02, 2009 6:16 pm

I would say it's correct mathematically, but I have a few comments:

1. Why are you transforming the scene instead of the sprite only? Is this intentional? Add another sprite to the scene to see what I mean. (Transforming the scene is equivalent to transforming the camera, btw)
2. The animation is not frame-independent. Change the timer interval to see what I mean. The right way to do it is to set a constant speed for the object and update the position according to the time passed. Or acceleration (for variable speed).
3. There's a small issue with the direction algo - the direction doesn't really lock to a value, but constantly adds +1 or -1 every step which causes the sprite to kinda jitter.

Using globals in the timer handler is actually easier on the garbage collector I think, but I might be wrong if lua creates a new point anyway and assigns a ref to the global. On the other hand using globals adds a global namespace lookup which is known to be slower than accessing local variables. There's a standard lua practice to assign a global to a local for faster (repeating) access.
Phil
 
Posts: 68
Joined: Fri Dec 28, 2007 12:47 am

Re: [WIP] Space Game

Postby Ivan on Fri Oct 02, 2009 6:54 pm

Phil wrote:Using globals in the timer handler is actually easier on the garbage collector

Number types in Lua are not garbage collected.
Ivan
Site Admin
 
Posts: 111
Joined: Thu Dec 27, 2007 9:21 pm

Re: [WIP] Space Game

Postby Phil on Fri Oct 02, 2009 9:20 pm

Ivan wrote:
Phil wrote:Using globals in the timer handler is actually easier on the garbage collector

Number types in Lua are not garbage collected.


I was talking about Point.
Phil
 
Posts: 68
Joined: Fri Dec 28, 2007 12:47 am

Re: [WIP] Space Game

Postby Virox on Sat Oct 03, 2009 7:43 am

Thx both for the replies.
I'll see what kind of adjustments I can make.

I've got one more question.
Is it better to create an triangle vector with the engine? or keep using a simple texture als my 'plane'?
Virox
 
Posts: 7
Joined: Tue Sep 29, 2009 5:27 pm

Re: [WIP] Space Game

Postby Phil on Sun Oct 04, 2009 2:20 am

It doesn't matter, at least from the engine's perspective.
Phil
 
Posts: 68
Joined: Fri Dec 28, 2007 12:47 am


Return to Showcase

Who is online

Users browsing this forum: No registered users and 1 guest

cron