~jordanreger/cc

ref: 70925adabf97ecfd96e1141590684a3891adf002 cc/xOS/.x -rw-r--r-- 2.1 KiB
70925adaJordan Reger feat: added "ring" page 3 months ago
                                                                                
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
local width, height = term.getSize()
local background_color = colors.lightGray

local window = window.create(term.current(), 1, 1, width, height)

local page = "home"

function setup()
    window.setBackgroundColor(background_color)
    if (background_color ~= colors.black and background_color ~= colors.gray) then
        window.setTextColor(colors.black)
    else
        window.setTextColor(colors.white)
    end
    window.clear()
end

function render()
    if page == "home" then
        render_home()
    end

    if page == "ring" then
        ring()
        page = "home"
    end
end

function render_home()
    window.clear()

    window.setCursorPos(width / 2 - 1, height / 2)
    window.write("Ring")
    window.setCursorPos(width / 2 - 3, height / 2 + 1)
    window.write("Shutdown")
end

function ring()
    rednet.open("back")

    local receiver_id = 17

    rednet.send(receiver_id, "gps")
    local r_id, r_pos = rednet.receive()

    local r_x, r_y, r_z = r_pos[1], r_pos[2], r_pos[3]
    local receiver = vector.new(r_x, r_y, r_z)

    local x, y, z = gps.locate()
    local client = vector.new(x, y, z)

    function update_pos()
        x, y, z = gps.locate()
        client = vector.new(x, y, z)
    end

    while true do
        local displacement = receiver - client
        if ((displacement.x > 0) and (displacement.x < 2)) and ((displacement.y < 2) and (displacement.y > 0)) and ((displacement.z > -1) and (displacement.z < 1)) then
            rednet.send(receiver_id, "open")
        end
        
        if((displacement.x < 0) and (displacement.x > -2)) and ((displacement.y < 2) and (displacement.y > 0)) and ((displacement.z > -1) and (displacement.z < 1)) then
            rednet.send(receiver_id, "open")
        end
        update_pos()
    end
end

function on_click()
    local e, id, x, y = os.pullEvent("mouse_click")
    if e then
        if (x >= width / 2 - 1 and x <= width / 2 + 2 and y == height / 2) then
            page = "ring"
        else
            return
        end
    end
end

function main()
    setup()
    
    render()
    on_click()

    sleep(0.25)
    main()
end

main()