-
Notifications
You must be signed in to change notification settings - Fork 0
/
rects.nelua
60 lines (48 loc) · 1.56 KB
/
rects.nelua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
--[[
Copyright (c) 2021-present André Luiz Alvares
Nene is licensed under the Zlib license.
Please refer to the LICENSE file for details
SPDX-License-Identifier: Zlib
]]
-- nene modules
local Nene = require 'nene.core'
local Color = require 'nene.color'
local Vec2 = require 'nene.math.vec2'
local Rectf = require 'nene.math.rectf'
local Intersections = require 'nene.intersections'
local Collision = require 'nene.collision'
-- example
local function rects()
local ok = Nene.init('nene rects', 256, 256)
assert(ok, 'error: nene initialization failed')
-- declare position and two rects
local rect_a: Rectf, rect_b: Rectf = { pos = {-32, -48}, size = { 32, 32} },
{ pos = {-64, 32}, size = {128, 64} }
repeat
Nene.update()
local delta_pos: Vec2
if Nene.is_scancode_held(SDL_SCANCODE_W) then
delta_pos.y = 1
end
if Nene.is_scancode_held(SDL_SCANCODE_S) then
delta_pos.y = -1
end
if Nene.is_scancode_held(SDL_SCANCODE_A) then
delta_pos.x = -1
end
if Nene.is_scancode_held(SDL_SCANCODE_D) then
delta_pos.x = 1
end
rect_a.pos = rect_a.pos + delta_pos
local collision = Collision.rectf_with_rectf(rect_a, rect_b, delta_pos)
if collision.collided then
rect_a.pos = rect_a.pos + collision.delta
end
Nene.render_clear(Color.bg)
Nene.render_draw_rect(rect_a:to_rect(), true, Color.white, true)
Nene.render_draw_rect(rect_b:to_rect(), true, Color.yellow, true)
Nene.render_present()
until Nene.should_quit()
Nene.terminate()
end
rects()