-
Notifications
You must be signed in to change notification settings - Fork 0
/
car.lua
137 lines (136 loc) · 3.22 KB
/
car.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
car={}
car.__index = car
setmetatable(car, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function car.new(positionX,positionY,length,width,topSpeed,acceleration,turnRate,friction,health)
local self = setmetatable({}, car)
self.startHealth=health
self.health=health
self.length=length
self.width=width
self.position={positionX,positionY}
self.startPosition=self.position[1]
self.collisionPoints = init
self.speed=0
self.angle=0
self.prevAngle=0
self.turnRate=turnRate
self.acceleration=acceleration+friction
self.topSpeed=topSpeed+.01
return self
end
function car:collisionDetect(map,mapX,preMap)
local mapx=mapX
local collision=false
for x=-self.width/2,self.width/2,2 do
for y=-self.length/2,self.length/2,4 do
local ang=math.deg(math.atan2(y,x))
local angle=ang+self.angle
local c=math.sqrt(y*y+x*x)
local newX=c*math.cos(math.rad(angle))+self.position[1]
local newY=c*math.sin(math.rad(angle))+self.position[2]
local tileY=math.ceil((love.graphics.getHeight()+mapX-newY)/32)
local tileX=math.ceil(newX/32)
if map:getTile(tileX,tileY)~=1 then
if collision==false then
if math.abs(self.speed)>1 then
self.health=math.ceil(self.health-3^(math.abs(self.speed)-1)+1)
end
if self.speed~=0 then
if y<1 then
self.position[1]=self.position[1]-math.sin(math.rad(self.angle))
mapx=mapx-math.cos(math.rad(self.angle))
else
self.position[1]=self.position[1]+math.sin(math.rad(self.angle))
mapx=mapx+math.cos(math.rad(self.angle))
end
self.speed=0
end
self.angle=self.prevAngle
collision=true
end
end
end
end
return mapx,collision
end
function car:turn(direction,dt)
local x=2
local v=1
if direction=="right" then
v=-1
end
if self.speed<0 then
x=-2
v=-v
end
self.prevAngle=self.angle
self.angle=self.angle-v*self.turnRate*dt*2/(self.speed+x)
end
function car:accelerate(acceleration,dt)
self.speed=self.speed+acceleration*self.acceleration*dt
if self.speed>self.topSpeed then
self.speed=self.topSpeed
elseif self.speed < -self.topSpeed/2 then
self.speed=-self.topSpeed/2
end
end
function car:getSpeed()
if self.speed>7.98 then
return 8
else
return self.speed
end
end
function car:getAngle()
return self.angle
end
function car:damage(damage)
self.health=self.health-damage
end
function car:getX()
return self.position[1]
end
function car:reset()
self.health=self.startHealth
self.position[1]=self.startPosition
self.speed=0
self.angle=0
end
function car:getY()
return self.position[2]
end
function car:draw(image)
love.graphics.draw(image,self.position[1],self.position[2],self.angle*math.pi/180,1,1,self.width/2,self.length/2)
end
function car:getAlive()
if self.health>0 then
return true
else
return false
end
end
function car:getHealth()
local health=self.health
if health<0 then
health=0
end
return health
end
function car:update(friction,dt)
self.position[1]=self.position[1]+math.sin(math.rad(self.angle))*self.speed
if self.speed < 0 then
friction=friction*(-1)
end
if self.speed==0 then
friction=0
end
if math.abs(self.speed)-friction*dt<(self.acceleration-friction)*dt/2 then
self.speed=0
friction=0
end
self.speed=self.speed-friction*dt
end