-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.lua
More file actions
65 lines (53 loc) · 2.01 KB
/
Copy pathball.lua
File metadata and controls
65 lines (53 loc) · 2.01 KB
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
local Object = require "classic"
require "util"
---@class Ball
---@field center {x: number, y: number}
---@field trueSize {height: number, width: number}
---@field velocity {x: number, y: number}
---@field imageSize {width: number, height: number}
---@field image string
---@field startPos {x: number, y:number}
local Ball = Object:extend()
Ball.scaleFactor = 0.125
local maxSpeed = 3
local accelForce = 0.1
function Ball:new()
self.center = {x = window.height/2, y=window.height/2}
self.velocity = {x = -.1, y = -.1}
self.acceleration = {x = 0, y = 0}
self.imageSize = {width = 128, height = 128}
self.image = love.graphics.newImage("assets/egg.png")
self.trueSize = {
width = 16,
height = 16
}
self.topLeft = {
x = 300 - 16,
y = 300 - 16,
}
self.startPos = {x = 0, y = 0}
end
function Ball:updatePos()
self.topLeft = {
x = self.center.x - self.trueSize.width/2,
y = self.center.y - self.trueSize.height/2
}
end
function Ball:updateMovement()
if (love.keyboard.isDown(worldCfg.keyBind.up)) then self.acceleration.y = -accelForce end
if (love.keyboard.isDown(worldCfg.keyBind.left)) then self.acceleration.x = -accelForce end
if (love.keyboard.isDown(worldCfg.keyBind.down)) then self.acceleration.y = accelForce end
if (love.keyboard.isDown(worldCfg.keyBind.right)) then self.acceleration.x = accelForce end
self.velocity.x = self.velocity.x + self.acceleration.x
self.velocity.y = self.velocity.y + self.acceleration.y
if (self.velocity.x > maxSpeed) then self.velocity.x = maxSpeed end
if (self.velocity.x < -maxSpeed) then self.velocity.x = -maxSpeed end
if (self.velocity.y > maxSpeed) then self.velocity.y = maxSpeed end
if (self.velocity.y < -maxSpeed) then self.velocity.y = -maxSpeed end
self.center.x = self.center.x + self.velocity.x
self.center.y = self.center.y + self.velocity.y
end
function Ball:__tostring()
return "ball: " .. self.topLeft.x .. ", " .. self.topLeft.y
end
return Ball