This repository was archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtile.lua
182 lines (155 loc) · 4.51 KB
/
tile.lua
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
local api, CHILDS, CONTENTS = ...
local W = 1920
local H = 1080
local PADDLE_SPEED = 0.5
local PADDLE_WIDTH = 220
local PADDLE_DEPTH = 20
local BALL_SIZE = 20
local BALL_X_SPEED = 5
local json = require "json"
local max, min, abs = math.max, math.min, math.abs
local running = false
local l, target_l
local r, target_r
local ball_x, ball_y, ball_speed, ball_vspeed
local score
local hiscore = 0
local num_players = 0
target_l = 50
target_r = 50
local pixel = resource.create_colored_texture(1,0,0, 0.6)
local font = resource.load_font(api.localized "font.ttf")
--
local remove_mapper = util.data_mapper{
[api.localized "pos"] = function(data)
local pkt = json.decode(data)
target_l = pkt.l
target_r = pkt.r
num_players = pkt.num
end;
}
local function restart(dir)
ball_x = W / 2 -- - (W/4) * dir
ball_y = H / 2
ball_speed = BALL_X_SPEED * dir
ball_vspeed = 0
l = target_l
r = target_r
score = 0
end
restart(-1)
local function p2y(percent) -- percent to y
return H / 100 * percent
end
local function move_to(target, current)
if current < target then
return current + min(PADDLE_SPEED, target-current)
else
return current - min(PADDLE_SPEED, current-target)
end
end
local function hit(paddle_y, paddle_speed)
ball_speed = ball_speed * -1
local off_center = (ball_y - paddle_y) / (PADDLE_WIDTH/2)
ball_vspeed = ball_vspeed + off_center
ball_vspeed = ball_vspeed + paddle_speed
if r ~= 50 or l ~= 50 then
score = score + 1
end
if score > hiscore then
hiscore = score
end
print("HIT HIT", off_center)
end
local function step_game()
l = move_to(target_l, l)
r = move_to(target_r, r)
ball_x = ball_x + ball_speed
ball_y = ball_y + ball_vspeed
if ball_y < 0 or ball_y > H then
ball_vspeed = ball_vspeed * -1
end
if ball_speed < 0 then
if ball_x < 0 then
restart(1)
end
if ball_x - BALL_SIZE/2 < PADDLE_DEPTH then -- left
local y = p2y(l)
local target_y = p2y(target_l)
if ball_y > y - PADDLE_WIDTH/2 - BALL_SIZE/2 and
ball_y < y + PADDLE_WIDTH/2 + BALL_SIZE/2 then
local moving = 0
if y - 10 > target_y then
moving = 1
elseif y + 10 < target_y then
moving = -1
end
hit(y, moving)
end
end
end
if ball_speed > 0 then
if ball_x > W then
restart(-1)
end
if ball_x + BALL_SIZE/2 > W - PADDLE_DEPTH then -- right
local y = p2y(r)
local target_y = p2y(target_r)
if ball_y > y - PADDLE_WIDTH/2 - BALL_SIZE/2 and
ball_y < y + PADDLE_WIDTH/2 + BALL_SIZE/2 then
local moving = 0
if y - 10 > target_y then
moving = 1
elseif y + 10 < target_y then
moving = -1
end
hit(y, moving)
end
end
end
end
local function draw_game()
local y
font:write(1420, 0, "33c3.infobeamer.com/pong", 30, 1,1,1,1)
font:write(1420, 30, "score " .. score .. ", best " .. hiscore .. ", players " .. num_players, 30, 1,1,1,1)
y = p2y(l)
pixel:draw(0, y - PADDLE_WIDTH/2, PADDLE_DEPTH, y + PADDLE_WIDTH/2)
-- gl.pushMatrix()
-- gl.translate(5, y + PADDLE_WIDTH/2)
-- gl.rotate(-90, 0, 0, 1)
-- font:write(0, 0, "https://33c3.infobeamer.com/pong", PADDLE_DEPTH/1.5, 1,1,1,1)
-- gl.popMatrix()
y = p2y(r)
pixel:draw(W-PADDLE_DEPTH, y - PADDLE_WIDTH/2, W, y + PADDLE_WIDTH/2)
-- gl.pushMatrix()
-- gl.translate(W-5, y - PADDLE_WIDTH/2)
-- gl.rotate(90, 0, 0, 1)
-- font:write(0, 0, "https://33c3.infobeamer.com/pong", PADDLE_DEPTH/1.5, 1,1,1,1)
-- gl.popMatrix()
pixel:draw(ball_x-BALL_SIZE/2, ball_y-BALL_SIZE/2,
ball_x+BALL_SIZE/2, ball_y+BALL_SIZE/2)
end
local M = {}
function M.updated_config_json(config)
running = false
for idx = 1, #config.games do
local game = config.games[idx]
if game.serial == sys.get_env("SERIAL") then
running = true
end
end
print("RUNNING STATUS FOR PONG", running)
end
function M.task(starts, ends, custom)
for now, x1, y1, x2, y2 in api.from_to(starts, ends) do
if running then
step_game()
draw_game()
end
end
end
function M.unload()
print "removing pong mapper"
remove_mapper()
end
return M