1-- GunFu Deadlands
2-- Copyright 2009-2011 Christiaan Janssen, September 2009-October 2011
3--
4-- This file is part of GunFu Deadlands.
5--
6--     GunFu Deadlands is free software: you can redistribute it and/or modify
7--     it under the terms of the GNU General Public License as published by
8--     the Free Software Foundation, either version 3 of the License, or
9--     (at your option) any later version.
10--
11--     GunFu Deadlands is distributed in the hope that it will be useful,
12--     but WITHOUT ANY WARRANTY; without even the implied warranty of
13--     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14--     GNU General Public License for more details.
15--
16--     You should have received a copy of the GNU General Public License
17--     along with GunFu Deadlands.  If not, see <http://www.gnu.org/licenses/>.
18
19Player = {}
20
21
22function Player.update( dt )
23	if Player.alive then
24		if Player.jumping then
25			Player.dir = {Player.spinning_dir[1], Player.spinning_dir[2]}
26			Player.speed = Player.speed_jumping
27		else
28			Player.dir = Player.get_dir_from_keys()
29			Player.speed = Player.speed_walking
30		end
31
32		--Movement.compute_predictions( Player )
33		Movement.update_character( dt, Player )
34
35		-- jumping!
36		if Player.jumping then
37			Player.jump_timer = Player.jump_timer - dt
38			if Player.jump_timer <= 0 then
39				Player.jumping = false
40			end
41		end
42
43		Player.update_fire( dt )
44
45		-- prediction
46		local accel = 1
47		if BulletTime.active then
48			accel = accel / BulletTime.slowdown_player
49		end
50		if Player.prediction_timer > 0 then
51			Player.prediction_timer = Player.prediction_timer - dt*accel
52		end
53
54	else
55		Movement.update_character( dt, Player )
56
57		if Player.death_timer > 0 then
58 			Player.death_timer = Player.death_timer - dt
59 		end
60	end
61end
62
63function Player.start_jumping()
64	if Player.readytojump and Player.alive and not Player.jumping and not (Player.dir[1]==0 and Player.dir[2]==0) then
65		Player.jumping = true
66		Player.readytojump = false
67		Player.spinning_dir = {Player.dir[1],Player.dir[2]}
68		Player.jump_timer = Graphics.get_jumptime()
69		Graphics.reset_jump()
70	end
71end
72
73
74function Player.set_jumping_done()
75	Player.readytojump = true
76end
77
78function Player.get_dir_from_keys()
79	local mydir = {0,0}
80	if love.keyboard.isDown( "s" ) or love.keyboard.isDown( "down" ) then
81		mydir[2] = 1
82	end
83
84	if love.keyboard.isDown( "w" ) or love.keyboard.isDown( "up" ) then
85		mydir[2] = -1
86	end
87
88	if love.keyboard.isDown( "a" ) or love.keyboard.isDown( "left" ) then
89		mydir[1] = -1
90	end
91
92	if love.keyboard.isDown( "d" ) or love.keyboard.isDown( "right" ) then
93		mydir[1] = 1
94	end
95
96	if mydir[1]*mydir[1]+mydir[2]*mydir[2] > 1 then
97		mydir = mymath.normalize_vector( mydir )
98	end
99
100	return mydir
101
102end
103
104function Player.start_reload()
105	if Player.reload_timer <= 0 and Player.bullet_pocket < Player.total_bullets then
106		Player.reload_timer = Player.bullet_loadingdelay
107		Sounds.play_reload_start()
108	end
109end
110
111function Player.update_fire( dt )
112	if Player.firing_timer>0 then
113		Player.firing_timer = Player.firing_timer - dt
114	end
115
116	if Player.reload_timer > 0 then
117		Player.reload_timer = Player.reload_timer - dt
118		if Player.reload_timer <= 0 then
119			-- reached 0: charge is done
120			if Level.onebullet then
121				Player.remaining_bullets = Player.remaining_bullets + Player.bullet_pocket
122				Player.bullet_pocket = Player.remaining_bullets
123				if Player.bullet_pocket > Player.total_bullets then
124					Player.bullet_pocket = Player.total_bullets
125				end
126				Player.remaining_bullets = Player.remaining_bullets - Player.bullet_pocket
127			else
128				Player.bullet_pocket = Player.total_bullets
129			end
130			Sounds.play_reload_done()
131		end
132	end
133
134
135	if Player.firing and Player.firing_timer<=0 then
136		if Player.bullet_pocket > 0 and Player.reload_timer<=0 then
137			-- new bullet
138			local lpos = {Player.pos[1], Player.pos[2]}
139			Bullets = Movement.newbullet_hero(lpos, {love.mouse.getX(), love.mouse.getY()})
140			if Player.firing_rate>0 then
141				Player.firing_timer = 1 / Player.firing_rate
142			end
143
144			Player.bullet_pocket = Player.bullet_pocket - 1
145
146			Sounds.play_shot_player()
147
148		else
149			Player.start_reload()
150		end
151
152	end
153
154end
155
156
157function Player.start_firing()
158	Player.firing = true
159end
160
161function Player.stop_firing()
162	Player.firing = false
163end
164
165function Player.get_prediction()
166	if Player.prediction_timer <= 0 then
167		Player.prediction_short = Movement.get_prediction( Player, Player.prediction_short_dt )
168		Player.prediction_timer = Player.prediction_delay*math.random()
169	end
170	return Player.prediction_short
171end
172