diff options
| author | jussi | 2023-11-23 00:00:49 +0200 |
|---|---|---|
| committer | jussi | 2023-11-23 00:00:49 +0200 |
| commit | 1ab9722875c543e8fd1b6600fd16e51412181641 (patch) | |
| tree | 412a3b0d139daa8832217e9e4240bd1cd299d4c5 /examples/snake | |
| parent | 85a48cf09302a2a14aeeb2d6cf3b8fcc1607e222 (diff) | |
| download | reilua-enhanced-1ab9722875c543e8fd1b6600fd16e51412181641.tar.gz reilua-enhanced-1ab9722875c543e8fd1b6600fd16e51412181641.tar.bz2 reilua-enhanced-1ab9722875c543e8fd1b6600fd16e51412181641.zip | |
Support for different platforms. Platform_desktop_sdl.
Diffstat (limited to 'examples/snake')
| -rw-r--r-- | examples/snake/main.lua | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/examples/snake/main.lua b/examples/snake/main.lua index 4273481..047cddf 100644 --- a/examples/snake/main.lua +++ b/examples/snake/main.lua @@ -1,5 +1,6 @@ package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua" +Util = require( "utillib" ) Vec2 = require( "vector2" ) Rect = require( "rectangle" ) @@ -174,14 +175,14 @@ end local function drawSnake() for i, seg in ipairs( snake.segments ) do - local angle = math.deg( RL.Vector2Angle( { 0, 0 }, seg.heading ) ) + local angle = -seg.heading:atan() + RL.PI / 2 local source = Rect:new( 16, 0, 8, 8 ) if i == 1 then -- Tail segment. Yes tail is actually the 'first' segment. source.x = 8 if 1 < #snake.segments then - angle = math.deg( RL.Vector2Angle( { 0, 0 }, snake.segments[ 2 ].heading ) ) + angle = -snake.segments[ 2 ].heading:atan() + RL.PI / 2 end elseif i < #snake.segments and seg.heading ~= snake.segments[ i+1 ].heading then -- Turned middle segments. source.x = 0 @@ -196,25 +197,27 @@ local function drawSnake() source.height = -8 end end + -- Notice that we set the origin to center { 4, 4 } that acts as pivot point. We also have to adjust our dest position by 4. RL.DrawTexturePro( snakeTexture, source, { seg.pos.x * TILE_SIZE + 4, seg.pos.y * TILE_SIZE + 4, 8, 8 }, { 4, 4 }, - angle, + angle * RL.RAD2DEG, RL.WHITE ) end -- Let's draw the head last to keep it on top. - local angle = math.deg( RL.Vector2Angle( { 0, 0 }, snake.heading ) ) + -- local angle = -math.atan2( snake.heading.x, snake.heading.y ) + RL.PI / 2 + local angle = -snake.heading:atan() + RL.PI / 2 RL.DrawTexturePro( snakeTexture, { 24, 0, 8, 8 }, { snake.headPos.x * TILE_SIZE + 4, snake.headPos.y * TILE_SIZE + 4, 8, 8 }, { 4, 4 }, - angle, + angle * RL.RAD2DEG, RL.WHITE ) end |
