|
1 | | -local timer = 0 |
2 | | -local window = { width = 1280, height = 720, } |
| 1 | +-------------------------------------------------------------------------------- |
| 2 | +--- LuaSTG basic example |
| 3 | +--- by Kuanlan@outlook.com |
| 4 | +-------------------------------------------------------------------------------- |
| 5 | + |
3 | 6 | local Keyboard = lstg.Input.Keyboard |
4 | 7 | local task = require("task") |
5 | 8 | local global_tasks = task.Manager() |
6 | | -local global_color = lstg.Color(255, 0, 0, 0) |
7 | | -local function set_camera() |
| 9 | + |
| 10 | +-------------------------------------------------------------------------------- |
| 11 | +--- camera |
| 12 | + |
| 13 | +local window = { width = 1280, height = 720, } |
| 14 | +local function applyCamera() |
8 | 15 | lstg.SetViewport(0, window.width, 0, window.height) |
9 | 16 | lstg.SetScissorRect(0, window.width, 0, window.height) |
10 | 17 | lstg.SetOrtho(0, window.width, 0, window.height) |
11 | 18 | lstg.SetImageScale(1) |
12 | 19 | lstg.SetFog() |
13 | 20 | end |
| 21 | + |
| 22 | +-------------------------------------------------------------------------------- |
| 23 | +--- game object |
| 24 | + |
| 25 | +local LAYER_PLAYER = 0 |
| 26 | +local LAYER_ENEMY_BULLET = 100 |
| 27 | +local GROUP_PLAYER = 1 |
| 28 | +local GROUP_ENEMY_BULLET = 2 |
| 29 | +local function clamp(v, a, b) |
| 30 | + return math.max(a, math.min(v, b)) |
| 31 | +end |
| 32 | +local player_class = { |
| 33 | + function(self) |
| 34 | + self.group = GROUP_PLAYER |
| 35 | + self.layer = LAYER_PLAYER |
| 36 | + self.img = "player-rect" |
| 37 | + self.rect = true |
| 38 | + self.a = 8 -- 16x16 |
| 39 | + self.b = 8 |
| 40 | + self.bound = false |
| 41 | + self.x = window.width / 2 |
| 42 | + self.y = window.height / 4 |
| 43 | + end, |
| 44 | + function(self) end, |
| 45 | + function(self) |
| 46 | + local dx = 0 |
| 47 | + local dy = 0 |
| 48 | + if Keyboard.GetKeyState(Keyboard.Left) and Keyboard.GetKeyState(Keyboard.Right) then |
| 49 | + dx = 0 |
| 50 | + elseif Keyboard.GetKeyState(Keyboard.Left) then |
| 51 | + dx = -1 |
| 52 | + elseif Keyboard.GetKeyState(Keyboard.Right) then |
| 53 | + dx = 1 |
| 54 | + end |
| 55 | + if Keyboard.GetKeyState(Keyboard.Down) and Keyboard.GetKeyState(Keyboard.Up) then |
| 56 | + dy = 0 |
| 57 | + elseif Keyboard.GetKeyState(Keyboard.Down) then |
| 58 | + dy = -1 |
| 59 | + elseif Keyboard.GetKeyState(Keyboard.Up) then |
| 60 | + dy = 1 |
| 61 | + end |
| 62 | + if dx ~= 0 and dy ~= 0 then |
| 63 | + local k = math.sqrt(2.0) * 0.5 |
| 64 | + dx = dx * k |
| 65 | + dy = dy * k |
| 66 | + end |
| 67 | + local speed = 4 |
| 68 | + if Keyboard.GetKeyState(Keyboard.LeftShift) then |
| 69 | + speed = 2 |
| 70 | + end |
| 71 | + self.x = clamp(self.x + speed * dx, 8, window.width - 8) |
| 72 | + self.y = clamp(self.y + speed * dy, 8, window.height - 8) |
| 73 | + end, |
| 74 | + lstg.DefaultRenderFunc, |
| 75 | + function(self, other) |
| 76 | + if other.group == GROUP_ENEMY_BULLET then |
| 77 | + lstg.Del(other) |
| 78 | + end |
| 79 | + end, |
| 80 | + function(self) end; |
| 81 | + is_class = true, |
| 82 | +} |
| 83 | +local bullet_class = { |
| 84 | + function(self) |
| 85 | + self.group = GROUP_ENEMY_BULLET |
| 86 | + self.layer = LAYER_ENEMY_BULLET |
| 87 | + self.img = "bullet-rect" |
| 88 | + self.rect = true |
| 89 | + self.a = 4 -- 16x16 / 2 |
| 90 | + self.b = 4 |
| 91 | + self.hscale = 0.5 |
| 92 | + self.vscale = 0.5 |
| 93 | + end, |
| 94 | + function(self) end, |
| 95 | + function(self) end, |
| 96 | + lstg.DefaultRenderFunc, |
| 97 | + function(self, other) end, |
| 98 | + function(self) end; |
| 99 | + is_class = true, |
| 100 | +} |
| 101 | +local function createBullet(x, y, a, v) |
| 102 | + local obj = lstg.New(bullet_class) |
| 103 | + obj.x = x |
| 104 | + obj.y = y |
| 105 | + lstg.SetV(obj, v, a, true) |
| 106 | + return obj |
| 107 | +end |
| 108 | +local function buildGameObjectScene() |
| 109 | + lstg.ResetPool() |
| 110 | + lstg.New(player_class) |
| 111 | + global_tasks:add(function() |
| 112 | + while true do |
| 113 | + local x, y = window.width / 2, window.height / 2 |
| 114 | + for i = 0, 239 do |
| 115 | + createBullet(x, y, (i / 240) * 360, 3) |
| 116 | + end |
| 117 | + task.wait(4) |
| 118 | + end |
| 119 | + end) |
| 120 | +end |
| 121 | +local function updateGameObject() |
| 122 | + lstg.ObjFrame() |
| 123 | + lstg.SetBound(0, window.width, 0, window.height) |
| 124 | + lstg.BoundCheck() |
| 125 | + lstg.CollisionCheck(GROUP_PLAYER, GROUP_ENEMY_BULLET) |
| 126 | + lstg.UpdateXY() |
| 127 | + lstg.AfterFrame() |
| 128 | +end |
| 129 | +local function renderGameObject() |
| 130 | + lstg.ObjRender() |
| 131 | +end |
| 132 | + |
| 133 | +-------------------------------------------------------------------------------- |
| 134 | +--- background |
| 135 | + |
| 136 | +local timer = 0 |
| 137 | +local function updateBackground() |
| 138 | + timer = timer + 1 |
| 139 | +end |
| 140 | +local function renderText(font, text, x, y, scale, color, oscale, ocolor, align) |
| 141 | + local d = oscale |
| 142 | + local k = oscale * math.sqrt(2.0) * 0.5 |
| 143 | + local s = 2 * scale |
| 144 | + lstg.RenderTTF(font, text, x - d, x - d, y, y, align, ocolor, s) |
| 145 | + lstg.RenderTTF(font, text, x + d, x + d, y, y, align, ocolor, s) |
| 146 | + lstg.RenderTTF(font, text, x, x, y - d, y - d, align, ocolor, s) |
| 147 | + lstg.RenderTTF(font, text, x, x, y + d, y + d, align, ocolor, s) |
| 148 | + lstg.RenderTTF(font, text, x - k, x - k, y + k, y + k, align, ocolor, s) |
| 149 | + lstg.RenderTTF(font, text, x + k, x + k, y + k, y + k, align, ocolor, s) |
| 150 | + lstg.RenderTTF(font, text, x + k, x + k, y - k, y - k, align, ocolor, s) |
| 151 | + lstg.RenderTTF(font, text, x - k, x - k, y - k, y - k, align, ocolor, s) |
| 152 | + lstg.RenderTTF(font, text, x, x, y, y, align, color, s) |
| 153 | +end |
| 154 | +local function renderBackground() |
| 155 | + local cx, cy = window.width / 2, window.height / 2 |
| 156 | + renderText("Sans", "海内存知己\n天涯若比邻\n欢迎来到 LuaSTG Sub", |
| 157 | + cx, cy, |
| 158 | + 1, lstg.Color(255, 2255, 255, 255), |
| 159 | + 4, lstg.Color(255, 0, 0, 0), |
| 160 | + 1 + 4) |
| 161 | + local circle_r = 300 |
| 162 | + for i = 0, (360 - 1), (360 / 60) do |
| 163 | + local angle = i + timer * 0.17 |
| 164 | + local cosv, sinv = math.cos(math.rad(angle)), math.sin(math.rad(angle)) |
| 165 | + local hscale = 16 + 15 * math.sin(math.rad(i * 11 + timer * 0.11)) |
| 166 | + local cr = circle_r + hscale * 8 |
| 167 | + lstg.Render("black-rect", cx + cr * cosv, cy + cr * sinv, angle, hscale, 1) |
| 168 | + end |
| 169 | +end |
| 170 | +local function drawDebugInfo() |
| 171 | + renderText("Sans", "移动/Move: ←↑↓→ 低速移动/Slower Move: LeftShift", |
| 172 | + 4, 4, |
| 173 | + 0.5, lstg.Color(255, 2255, 255, 255), |
| 174 | + 2, lstg.Color(255, 0, 0, 0), |
| 175 | + 0 + 8) |
| 176 | + local text = string.format("OBJ %d FPS %.2f", lstg.GetnObj(), lstg.GetFPS()) |
| 177 | + renderText("Sans", text, |
| 178 | + window.width - 4, 4, |
| 179 | + 0.5, lstg.Color(255, 2255, 255, 255), |
| 180 | + 2, lstg.Color(255, 0, 0, 0), |
| 181 | + 2 + 8) |
| 182 | +end |
| 183 | + |
| 184 | +-------------------------------------------------------------------------------- |
| 185 | +--- framework |
| 186 | + |
14 | 187 | function GameInit() |
15 | 188 | lstg.ChangeVideoMode(window.width, window.height, true, false) |
16 | | - set_camera() |
| 189 | + applyCamera() |
17 | 190 | lstg.LoadTexture("white", "res/white.png", false) |
18 | 191 | lstg.LoadImage("white", "white", 0, 0, 16, 16) |
| 192 | + lstg.LoadImage("black-rect", "white", 0, 0, 16, 16) |
| 193 | + lstg.SetImageState("black-rect", "", lstg.Color(255, 0, 0, 0)) |
| 194 | + lstg.LoadImage("player-rect", "white", 0, 0, 16, 16) |
| 195 | + lstg.SetImageState("player-rect", "", lstg.Color(255, 64, 64, 255)) |
| 196 | + lstg.LoadImage("bullet-rect", "white", 0, 0, 16, 16) |
| 197 | + lstg.SetImageState("bullet-rect", "", lstg.Color(96, 0, 0, 0)) |
19 | 198 | if not lstg.LoadTTF("Sans", "C:/Windows/Fonts/msyh.ttc", 48, 48) then |
20 | 199 | lstg.LoadTTF("Sans", "C:/Windows/Fonts/msyh.ttf", 48, 48) -- Windows 7 |
21 | 200 | end |
22 | | - global_tasks:add(function() |
23 | | - while true do |
24 | | - for i = 1, 60 do |
25 | | - global_color.r = (i / 60) * 128 |
26 | | - global_color.b = (i / 60) * 128 |
27 | | - task.wait(1) |
28 | | - end |
29 | | - for i = 59, 0, -1 do |
30 | | - global_color.r = (i / 60) * 128 |
31 | | - global_color.b = (i / 60) * 128 |
32 | | - task.wait(1) |
33 | | - end |
34 | | - global_tasks:add(function() |
35 | | - for i = 1, 15 do |
36 | | - global_color.g = (i / 15) * 192 |
37 | | - task.wait(1) |
38 | | - end |
39 | | - for i = 14, 0, -1 do |
40 | | - global_color.g = (i / 15) * 192 |
41 | | - task.wait(1) |
42 | | - end |
43 | | - end) |
44 | | - end |
45 | | - end) |
| 201 | + buildGameObjectScene() |
46 | 202 | end |
47 | 203 | function GameExit() |
48 | 204 | end |
49 | 205 | function FrameFunc() |
50 | 206 | global_tasks:remove_dead() |
51 | 207 | global_tasks:resume_all() |
52 | | - lstg.ObjFrame() |
53 | | - lstg.BoundCheck() |
54 | | - lstg.CollisionCheck(1, 2) -- group 1 and group 2 |
55 | | - lstg.UpdateXY() |
56 | | - lstg.AfterFrame() |
57 | | - timer = timer + 1 |
| 208 | + updateBackground() |
| 209 | + updateGameObject() |
58 | 210 | if Keyboard.GetKeyState(Keyboard.Escape) then |
59 | 211 | return true -- exit |
60 | 212 | end |
|
63 | 215 | function RenderFunc() |
64 | 216 | lstg.BeginScene() |
65 | 217 | lstg.RenderClear(lstg.Color(255, 255, 255, 255)) |
66 | | - set_camera() |
67 | | - lstg.ObjRender() |
68 | | - local cx, cy = window.width / 2, window.height / 2 |
69 | | - lstg.RenderTTF("Sans", "海内存知己\n天涯若比邻\n欢迎来到 LuaSTG Sub", cx, cx, cy, cy, 1 + 4, lstg.Color(255, 0, 0, 0), 2) |
70 | | - lstg.SetImageState("white", "", global_color) |
71 | | - local circle_r = 300 |
72 | | - for i = 0, (360 - 1), (360 / 60) do |
73 | | - local angle = i + timer * 0.17 |
74 | | - local cosv, sinv = math.cos(math.rad(angle)), math.sin(math.rad(angle)) |
75 | | - local hscale = 16 + 15 * math.sin(math.rad(i * 11 + timer * 0.11)) |
76 | | - local cr = circle_r + hscale * 8 |
77 | | - lstg.Render("white", cx + cr * cosv, cy + cr * sinv, angle, hscale, 1) |
78 | | - end |
| 218 | + applyCamera() |
| 219 | + renderGameObject() |
| 220 | + renderBackground() |
| 221 | + drawDebugInfo() |
79 | 222 | lstg.EndScene() |
80 | 223 | end |
0 commit comments