Compare commits
9 commits
b29e172b7e
...
d56b42e017
| Author | SHA1 | Date | |
|---|---|---|---|
| d56b42e017 | |||
| 4d2874c6be | |||
| 64c825c84e | |||
| 9479825078 | |||
| 465258b240 | |||
| 2e73c6c332 | |||
| 0fff61f93c | |||
| 4a149be546 | |||
| a19b2ce0a0 |
6 changed files with 116 additions and 7 deletions
45
.vscode/gameobject.code-snippets
vendored
Normal file
45
.vscode/gameobject.code-snippets
vendored
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
{
|
||||
// Place your learns workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
|
||||
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
|
||||
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
|
||||
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
|
||||
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
|
||||
// Placeholders with the same ids are connected.
|
||||
// Example:
|
||||
// "Print to console": {
|
||||
// "scope": "javascript,typescript",
|
||||
// "prefix": "log",
|
||||
// "body": [
|
||||
// "console.log('$1');",
|
||||
// "$2"
|
||||
// ],
|
||||
// "description": "Log output to console"
|
||||
// }
|
||||
"GameObject Extension": {
|
||||
"scope": "lua",
|
||||
"prefix": "extendGameObject",
|
||||
"body": [
|
||||
"---@class ${1:ClassName}:GameObject",
|
||||
"local ${1:ClassName} = GameObject:extend()",
|
||||
"",
|
||||
"function ${1:ClassName}:new(area, x, y, opts)",
|
||||
" ${1:ClassName}.super.new(self, area, x, y, opts)",
|
||||
"end",
|
||||
"",
|
||||
"---Updates ${1:ClassName}",
|
||||
"---@param dt number delta time",
|
||||
"function ${1:ClassName}:update(dt)",
|
||||
" ${1:ClassName}.super.update(self, dt)",
|
||||
"end",
|
||||
"",
|
||||
"---Draws ${1:ClassName}",
|
||||
"function ${1:ClassName}:draw()",
|
||||
" ${1:ClassName}.super.draw(self)",
|
||||
"end",
|
||||
"",
|
||||
"return ${1:ClassName}",
|
||||
""
|
||||
],
|
||||
"description": "Extends GameObject with a LuaLS class definition and implements new, update, and draw."
|
||||
}
|
||||
}
|
||||
22
main.lua
22
main.lua
|
|
@ -53,7 +53,9 @@ function love.load()
|
|||
up = {'key:up', 'key:w'},
|
||||
down = {'key:down', 'key:s'},
|
||||
action = {'key:z', 'key:space'},
|
||||
f1 = {'key:f1'}, f2 = {'key:f2'}, f3 = {'key:f3'}, f12 = {'key:f12'}
|
||||
|
||||
f1 = {'key:f1'}, f2 = {'key:f2'}, f3 = {'key:f3'},
|
||||
f10 = {'key:f10'}, f11 = {'key:f11'}, f12 = {'key:f12'}
|
||||
},
|
||||
pairs = {
|
||||
move = {'left', 'right', 'up', 'down'}
|
||||
|
|
@ -68,6 +70,10 @@ function love.update(dt)
|
|||
if current_room then current_room:update(dt) end
|
||||
input:update(dt)
|
||||
if input:pressed('f1') then gotoRoom('CircleRoom') end
|
||||
if input:pressed('f2') then
|
||||
-- debug breakpoint
|
||||
print('break')
|
||||
end
|
||||
if input:pressed('f3') then camera:shake(4, 60, 1) end
|
||||
if input:pressed('f12') then
|
||||
print("-------------------------------------")
|
||||
|
|
@ -152,6 +158,20 @@ function resize(s)
|
|||
sx, sy = s, s
|
||||
end
|
||||
|
||||
---Pushes a transformation stack for rotating and/or scaling
|
||||
---@param x number horizontal coordinate to translate
|
||||
---@param y number vertical coordinate to translate
|
||||
---@param r number|nil angle to rotate
|
||||
---@param sx number|nil horizontal scale
|
||||
---@param sy number|nil vertical scale
|
||||
function pushRotateScale(x, y, r, sx, sy)
|
||||
love.graphics.push()
|
||||
love.graphics.translate(x, y)
|
||||
love.graphics.rotate(r or 0)
|
||||
love.graphics.scale(sx or 1, sy or sx or 1)
|
||||
love.graphics.translate(-x, -y)
|
||||
end
|
||||
|
||||
---Returns random number
|
||||
---@param min number minimum of range
|
||||
---@param max number maximum of range
|
||||
|
|
|
|||
|
|
@ -6,6 +6,10 @@
|
|||
---@field y number
|
||||
---@field opts table|nil
|
||||
---@field super GameObject
|
||||
---@field id string
|
||||
---@field dead boolean
|
||||
---@field timer Timer
|
||||
---@field collider table|nil
|
||||
local GameObject = Object:extend()
|
||||
|
||||
---Instantiates game object
|
||||
|
|
|
|||
|
|
@ -1,3 +1,9 @@
|
|||
--[[
|
||||
Generally whenever you want to get a position B that is distance units away from position A such that position B is positioned at a specific angle in relation to position A, the pattern is something like:
|
||||
|
||||
bx = ax + distance*math.cos(angle) and by = ay + distance*math.sin(angle).
|
||||
]]
|
||||
|
||||
---@class Player:GameObject
|
||||
---@field area Area
|
||||
---@field x number
|
||||
|
|
@ -21,6 +27,8 @@ function Player:new(area, x, y, opts)
|
|||
self.max_v = 100
|
||||
-- player acceleration
|
||||
self.a = 100
|
||||
|
||||
self.timer:every(4, function() self:shoot() end)
|
||||
end
|
||||
|
||||
function Player:update(dt)
|
||||
|
|
@ -43,7 +51,7 @@ function Player:update(dt)
|
|||
-- move collision area
|
||||
self.collider:moveTo(self.x, self.y)
|
||||
|
||||
if input:pressed('f2') then self:kill() end
|
||||
if input:pressed('f11') then self:kill() end
|
||||
end
|
||||
|
||||
function Player:draw()
|
||||
|
|
@ -56,4 +64,10 @@ function Player:draw()
|
|||
love.graphics.circle('line', self.x, self.y, self.w)
|
||||
end
|
||||
|
||||
function Player:shoot()
|
||||
local d = 1.2*self.w
|
||||
local offset_x, offset_y = self.x + d*math.cos(self.r), self.y + d*math.sin(self.r)
|
||||
self.area:addGameObject('ShootEffect', offset_x, offset_y, {player = self, d = d})
|
||||
end
|
||||
|
||||
return Player
|
||||
|
|
|
|||
31
obj/game/ShootEffect.lua
Normal file
31
obj/game/ShootEffect.lua
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
---@class ShootEffect:GameObject
|
||||
---@field player Player|nil
|
||||
---@field d number
|
||||
local ShootEffect = GameObject:extend()
|
||||
|
||||
function ShootEffect:new(area, x, y, opts)
|
||||
ShootEffect.super.new(self, area, x, y, opts)
|
||||
self.w = 8
|
||||
self.timer:tween(0.4, self, {w = 0}, 'in-out-cubic', function() self:kill() end)
|
||||
end
|
||||
|
||||
---Updates ShootEffect
|
||||
---@param dt number delta time
|
||||
function ShootEffect:update(dt)
|
||||
ShootEffect.super.update(self, dt)
|
||||
if self.player then
|
||||
self.x = self.player.x + self.d*math.cos(self.player.r)
|
||||
self.y = self.player.y + self.d*math.sin(self.player.r)
|
||||
end
|
||||
end
|
||||
|
||||
---Draws ShootEffect
|
||||
function ShootEffect:draw()
|
||||
ShootEffect.super.draw(self)
|
||||
pushRotateScale(self.x, self.y, self.player.r+math.pi/4)
|
||||
love.graphics.setColor(0.6, 0.5, 0.1)
|
||||
love.graphics.rectangle('fill', self.x - self.w/2, self.y - self.w/2, self.w, self.w)
|
||||
love.graphics.pop()
|
||||
end
|
||||
|
||||
return ShootEffect
|
||||
|
|
@ -35,11 +35,6 @@ function CircleRoom:update(dt)
|
|||
end
|
||||
end
|
||||
|
||||
if input:pressed('up') then
|
||||
-- debug breakpoint
|
||||
print('break')
|
||||
end
|
||||
|
||||
self.timer:update(dt)
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue