1-- This file is a part of Avoision.
2--
3-- Copyright (C) 2011 Jared Krinke.
4--
5-- Avoision is free software; you can redistribute it and/or
6-- modify it under the terms of the GNU General Public License
7-- as published by the Free Software Foundation; either version 2
8-- of the License, or (at your option) any later version.
9--
10-- This program is distributed in the hope that it will be useful,
11-- but WITHOUT ANY WARRANTY; without even the implied warranty of
12-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13-- GNU General Public License for more details.
14--
15-- You should have received a copy of the GNU General Public License
16-- along with this program; if not, write to the Free Software
17-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19require("Avoision/Common.lua")
20require("Effects/Ghost.lua")
21
22local playerVelocity = 0.6 / 1000
23local playerWidth = 1 / 30
24local playerHeight = 1 / 30
25
26local playerGhostPeriod = 750
27local playerGhostScaleMax = 6
28
29Player = { }
30
31function Player.new()
32    local player = Entity.new(0, 0, 0, playerWidth, playerHeight)
33
34    player.color = Common.playerColor
35    player.mesh = Common.squareMesh
36    player.group = Common.group.player
37    player.v = { 0, 0, 0, 0 }
38
39    player.elements:add(Element.Image.new("Images/Block.png", 0, 0, 0, 1, 1, 0))
40
41    function player.setMovingUpState(self, moving)
42        if moving then
43            self.v[1] = 1
44        else
45            self.v[1] = 0
46        end
47    end
48
49    function player.setMovingDownState(self, moving)
50        if moving then
51            self.v[2] = 1
52        else
53            self.v[2] = 0
54        end
55    end
56
57    function player.setMovingLeftState(self, moving)
58        if moving then
59            self.v[3] = 1
60        else
61            self.v[3] = 0
62        end
63    end
64
65    function player.setMovingRightState(self, moving)
66        if moving then
67            self.v[4] = 1
68        else
69            self.v[4] = 0
70        end
71    end
72
73    function player.clearMovingStates(self)
74        self.v[1] = 0
75        self.v[2] = 0
76        self.v[3] = 0
77        self.v[4] = 0
78    end
79
80    function player.update(self, ms)
81        local directionX = self.v[4] - self.v[3]
82        local directionY = self.v[1] - self.v[2]
83
84        if directionX ~= 0 or directionY ~= 0 then
85            local direction = arctan(directionY, directionX)
86
87            self.x = self.x + playerVelocity * ms * cos(direction)
88            self.y = self.y + playerVelocity * ms * sin(direction)
89
90            if abs(self.x) + self.width / 2 > 0.5 then
91                if self.x > 0 then
92                    self.x = 0.5 - self.width / 2
93                else
94                    self.x = -0.5 + self.width / 2
95                end
96            end
97
98            if abs(self.y) + self.height / 2 > 0.5 then
99                if self.y > 0 then
100                    self.y = 0.5 - self.height / 2
101                else
102                    self.y = -0.5 + self.height / 2
103                end
104            end
105        end
106    end
107
108    function player.createGhost(self)
109        return Ghost.new(self, playerGhostPeriod, playerGhostScaleMax)
110    end
111
112    return player
113end
114
115