]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CTrain.cpp
Added .gitignore to ignore certain files + fixed access rights on Makefile* as
[quix0rs-blobwars.git] / src / CTrain.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3 Copyright (C) 2011-2015 Perpendicular Dimensions
4
5 This program 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.
13
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 */
21
22 #include "headers.h"
23
24 Train::Train()
25 {
26         active = true;
27         type = TR_TRAIN;
28         next = NULL;
29         sprite = NULL;
30         waitAtStart = true;
31 }
32
33 float Train::getDX()
34 {
35         if ((think > 0) || (!active))
36                 return 0;
37
38         return dx;
39 }
40
41 float Train::getDY()
42 {
43         if ((think > 0) || (!active))
44                 return 0;
45
46         return dy;
47 }
48
49 bool Train::isReady()
50 {
51         if (think == 0)
52         {
53                 think = 2;
54                 return true;
55         }
56
57         think = 2;
58
59         return false;
60 }
61
62 bool Train::isMoving()
63 {
64         if (think == 0)
65                 return true;
66                 
67         return false;
68 }
69
70 bool Train::waitsForPlayer()
71 {
72         if ((active) || (pause != 0))
73                 return false;
74
75         return true;
76 }
77
78 void Train::setName(const char *name)
79 {
80         strlcpy(this->name, name, sizeof this->name);
81 }
82
83 void Train::set(int startX, int startY, int endX, int endY, int pause, bool fromStart)
84 {
85         this->startX = startX;
86         this->startY = startY;
87         this->endX = endX;
88         this->endY = endY;
89
90         this->pause = pause;
91         this->think = 0;
92
93         Math::calculateSlope(startX, startY, endX, endY, &dx, &dy);
94         
95         if ((dx != 0) && (dy != 0))
96                 debug(("WARNING: TRAIN '%s' is not straight! - %d %d %d %d\n", name, startX, startY, endX, endY));
97
98         if (fromStart)
99         {
100                 this->x = this->startX;
101                 this->y = this->startY;
102                 waitAtStart = true;
103         }
104         else
105         {
106                 this->x = this->endX;
107                 this->y = this->endY;
108                 if (type == TR_TRAIN)
109                 {
110                         dx = -dx;
111                         dy = -dy;
112                 }
113                 waitAtStart = false;
114         }
115 }
116
117 bool Train::openClose()
118 {
119         Math::limitInt(&(--think), 0, 9999);
120
121         if (active)
122         {
123                 if ((x > startX) || (y > startY))
124                 {
125                         x += dx;
126                         y += dy;
127                         
128                         if ((x == startX) && (y == startY))
129                                 return true;
130                 }
131
132                 return false;
133         }
134         else
135         {
136                 if ((x < endX) || (y < endY))
137                 {
138                         x -= dx;
139                         y -= dy;
140
141                         if ((x == endX) && (y == endY))
142                                 return true;
143                 }
144                 
145                 return false;
146         }
147 }
148
149 void Train::move()
150 {
151         if (think > 0)
152         {
153                 think--;
154                 return;
155         }
156
157         if (!active)
158                 return;
159
160         x -= dx;
161         y -= dy;
162
163         if (((x >= endX) && (y >= endY)) || ((x <= startX) && (y <= startY)))
164         {
165                 dx = -dx;
166                 dy = -dy;
167                 think = pause;
168                 
169                 if (think == 0)
170                 {
171                         think = 60;
172
173                         if ((x <= startX) && (y <= startY) && (waitAtStart))
174                         {
175                                 active = false;
176                                 think = 0;
177                         }
178
179                         if ((x >= endX) && (y >= endY) && (!waitAtStart))
180                         {
181                                 active = false;
182                                 think = 0;
183                         }
184                 }
185         }
186 }
187
188 int Train::getPause()
189 {
190         return pause;
191 }