-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.lua
131 lines (99 loc) · 2.71 KB
/
Player.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
local Player = class('Player');
local Projectile = require('./Projectile');
local ship = loadModel("./models/ship.obj");
local RELOAD_TIME = 0.1;
function Player:init(x, y, z)
self.pos = createVector(x, y, z);
self.vel = createVector(0, 0, 30);
self.angle = createVector(0, 0, 0);
self.angleVel = createVector(1, 0, 0);
self.reloadTimer = 0;
self.projectiles = {};
self.is_hit = false;
self.hit_timer = 0;
return self;
end
function Player:shoot()
table.insert(self.projectiles, 1, Projectile:new(
self.pos.x, -- Pass player position
self.pos.y - 12,
440,
-self.vel.x/3, -- Pass player velocity
-self.vel.y/3,
self.vel.z
));
self.reloadTimer = 0;
end
function Player:draw()
push();
translate(0, 200, 50);
rotateY(90);
rotateZ(self.angle.z);
rotateX(180);
rotateX(self.angle.x);
scale(8);
fill(230, 255, 100);
stroke(195, 175, 10);
strokeWeight(4);
if (self.is_hit) then
fill(255, 175, 100);
self.angleVel.x = self.angleVel.x + random(-3, 3);
self.angleVel.z = self.angleVel.z + random(-3, 3);
if (self.hit_timer > (500 * deltaTime)) then
self.is_hit = false;
self.hit_timer = 0;
end
self.hit_timer = self.hit_timer +1
end
model(ship);
pop();
for i, proj in ipairs(self.projectiles) do
proj:draw(self.pos.x, self.pos.y-100);
end
end
function Player:update()
-- Apply velocities
self.pos.x = self.pos.x + self.vel.x;
self.pos.y = self.pos.y + self.vel.y;
-- Apply angle velocity
self.angle.x = self.angle.x + self.angleVel.x;
self.angle.z = self.angle.z + self.angleVel.z;
-- Angle velocity friction
self.angleVel.x = self.angleVel.x * 0.2;
self.angleVel.z = self.angleVel.z * 0.2;
-- Reset ship to initial angle
self.angle.x = self.angle.x * 0.8;
self.angle.z = self.angle.z * 0.8;
-- Left/Right
if (keyIsDown('a')) then
self.angleVel.x = self.angleVel.x + 6;
self.vel.x = self.vel.x - 35*deltaTime;
end
if (keyIsDown('d')) then
self.angleVel.x = self.angleVel.x - 6;
self.vel.x = self.vel.x + 35*deltaTime;
end
-- Up/Down Pitch control
if (keyIsDown('w')) then
self.angleVel.z = self.angleVel.z - 4;
self.vel.y = self.vel.y - 35*deltaTime;
end
if (keyIsDown('s')) then
self.angleVel.z = self.angleVel.z + 4;
self.vel.y = self.vel.y + 35*deltaTime;
end
if (self.vel.z < 60) then
self.vel.z = self.vel.z + 1*deltaTime
end;
self.vel.x = self.vel.x * 0.95;
self.vel.y = self.vel.y * 0.95;
-- Shoot
if (keyIsDown(32) and self.reloadTimer > RELOAD_TIME) then
self:shoot();
end
self.reloadTimer = self.reloadTimer + 1*deltaTime;
for i, proj in ipairs(self.projectiles) do
proj:update();
end
end
return Player