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
