diff --git a/firmware/main/sdcard/spacewar/spacewar.lua b/firmware/main/sdcard/spacewar/spacewar.lua index d71e88e7..4b5557f1 100644 --- a/firmware/main/sdcard/spacewar/spacewar.lua +++ b/firmware/main/sdcard/spacewar/spacewar.lua @@ -6,10 +6,13 @@ MAGENTA = display.color565(255, 0, 255) ROOT = 'spacewar/' +ANGLE_COUNT = 60 +ANGLE_STEP = 360 / ANGLE_COUNT + ship_sprites = { resources.load_bitmap(ROOT .. "ship.bmp", MAGENTA) } -for i = 2, 12 do +for i = 2, ANGLE_COUNT do local angle = i - 1 - ship_sprites[i] = resources.rotate_bitmap(ship_sprites[1], angle * 30, MAGENTA) + ship_sprites[i] = resources.rotate_bitmap(ship_sprites[1], angle * ANGLE_STEP, MAGENTA) end Ship = { @@ -18,7 +21,14 @@ Ship = { width = 32, -- Розмір спрайту - 32x32 height = 32, sprites = ship_sprites, - rotation = 0, + rotation = 0, -- Поворот корабля в градусах + speed_x = 0, + speed_y = 0, + accel_forward = 0, + angular_speed = 0, + max_speed_x = display.width, + max_speed_y = display.height, + -- accel_y = 0, } function Ship:new(o) @@ -28,19 +38,45 @@ function Ship:new(o) return o end +function Ship:set_forward_acceleration(accel) + self.accel_forward = accel +end + +function Ship:set_angular_speed(speed) + self.angular_speed = speed +end + function Ship:update(delta) - -- self.x = self.x + 50 * delta - self.rotation = (self.rotation + 1) % #self.sprites + local dir_x = math.cos(math.rad(-self.rotation)) + local dir_y = math.sin(math.rad(-self.rotation)) + + self.speed_x = self.speed_x + self.accel_forward * delta * dir_x + self.speed_y = self.speed_y + self.accel_forward * delta * dir_y + + self.speed_x = math.clamp(self.speed_x, -self.max_speed_x, self.max_speed_x) + self.speed_y = math.clamp(self.speed_y, -self.max_speed_y, self.max_speed_y) + + self.x = self.x + self.speed_x * delta + self.y = self.y + self.speed_y * delta + + self.x = self.x % display.width + self.y = self.y % display.height + + self.rotation = (self.rotation + self.angular_speed * delta) % 360 end function Ship:draw() - display.draw_bitmap(self.sprites[self.rotation + 1], self.x - self.width / 2, self.y - self.height / 2) + local rotation_index = math.floor(self.rotation / ANGLE_STEP) + 1 + -- Координати верхнього лівого кута спрайту + local cx = math.floor(self.x - self.width / 2) + local cy = math.floor(self.y - self.height / 2) + display.draw_bitmap(self.sprites[rotation_index], cx, cy) -- Якщо ми біля краю екрану, малюємо корабель ще раз на протилежному боці if self.x < self.width / 2 or self.x > display.width - self.width / 2 then - display.draw_bitmap(self.sprites[self.rotation + 1], self.x - self.width / 2 + display.width, self.y - self.height / 2) + display.draw_bitmap(self.sprites[rotation_index], cx + display.width, cy) end if self.y < self.height / 2 or self.y > display.height - self.height / 2 then - display.draw_bitmap(self.sprites[self.rotation + 1], self.x - self.width / 2, self.y - self.height / 2 + display.height) + display.draw_bitmap(self.sprites[rotation_index], cx, cy + display.height) end end @@ -54,7 +90,34 @@ function lilka._update(delta) ship:update(delta) ship:draw() display.render() - if controller.get_state().a.just_pressed then + local state = controller.get_state() + -- if state.left.pressed then + -- ship.rotate(-1 * delta) + -- ship.rotation = (ship.rotation - 1) % #ship.sprites + -- end + + -- if state.up.pressed then + -- ship:set_forward_acceleration(50) + -- elseif state.down.pressed then + -- -- ship:set_forward_acceleration(-50) + -- else + -- ship:set_forward_acceleration(0) + -- end + + if state.up.pressed then + ship:set_forward_acceleration(50) + else + ship:set_forward_acceleration(0) + end + + if state.down.pressed then + ship:set_angular_speed(90) + else + ship:set_angular_speed(0) + end + + + if state.a.just_pressed then util.exit() end end diff --git a/sdk/lib/lilka/src/lua/luarunner.cpp b/sdk/lib/lilka/src/lua/luarunner.cpp index 4768bfde..ea952427 100644 --- a/sdk/lib/lilka/src/lua/luarunner.cpp +++ b/sdk/lib/lilka/src/lua/luarunner.cpp @@ -46,7 +46,7 @@ int execute(lua_State* L) { lua_getfield(L, -1, "_update"); if (lua_isfunction(L, -1)) { // Call _update function, passing delta as argument - lua_pushnumber(L, delta); + lua_pushnumber(L, delta / 1000.0); retCode = lua_pcall(L, 1, 0, 0); if (retCode) { // const char* err = lua_tostring(L, -1);