]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/CTrain.cpp
Don't link pak tool with SDL.
[quix0rs-blobwars.git] / src / CTrain.cpp
1 /*
2 Copyright (C) 2004-2011 Parallel Realities
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #include "headers.h"
22
23 Train::Train()
24 {
25         active = true;
26         type = TR_TRAIN;
27         next = NULL;
28         sprite = NULL;
29         waitAtStart = true;
30 }
31
32 float Train::getDX()
33 {
34         if ((think > 0) || (!active))
35                 return 0;
36
37         return dx;
38 }
39
40 float Train::getDY()
41 {
42         if ((think > 0) || (!active))
43                 return 0;
44
45         return dy;
46 }
47
48 bool Train::isReady()
49 {
50         if (think == 0)
51         {
52                 think = 2;
53                 return true;
54         }
55
56         think = 2;
57
58         return false;
59 }
60
61 bool Train::isMoving()
62 {
63         if (think == 0)
64                 return true;
65                 
66         return false;
67 }
68
69 bool Train::waitsForPlayer()
70 {
71         if ((active) || (pause != 0))
72                 return false;
73
74         return true;
75 }
76
77 void Train::setName(const char *name)
78 {
79         strlcpy(this->name, name, sizeof this->name);
80 }
81
82 void Train::set(int startX, int startY, int endX, int endY, int pause, bool fromStart)
83 {
84         this->startX = startX;
85         this->startY = startY;
86         this->endX = endX;
87         this->endY = endY;
88
89         this->pause = pause;
90         this->think = 0;
91
92         Math::calculateSlope(startX, startY, endX, endY, &dx, &dy);
93         
94         if ((dx != 0) && (dy != 0))
95                 debug(("WARNING: TRAIN '%s' is not straight! - %d %d %d %d\n", name, startX, startY, endX, endY));
96
97         if (fromStart)
98         {
99                 this->x = this->startX;
100                 this->y = this->startY;
101                 waitAtStart = true;
102         }
103         else
104         {
105                 this->x = this->endX;
106                 this->y = this->endY;
107                 if (type == TR_TRAIN)
108                 {
109                         dx = -dx;
110                         dy = -dy;
111                 }
112                 waitAtStart = false;
113         }
114 }
115
116 bool Train::openClose()
117 {
118         Math::limitInt(&(--think), 0, 9999);
119
120         if (active)
121         {
122                 if ((x > startX) || (y > startY))
123                 {
124                         x += dx;
125                         y += dy;
126                         
127                         if ((x == startX) && (y == startY))
128                                 return true;
129                 }
130
131                 return false;
132         }
133         else
134         {
135                 if ((x < endX) || (y < endY))
136                 {
137                         x -= dx;
138                         y -= dy;
139
140                         if ((x == endX) && (y == endY))
141                                 return true;
142                 }
143                 
144                 return false;
145         }
146 }
147
148 void Train::move()
149 {
150         if (think > 0)
151         {
152                 think--;
153                 return;
154         }
155
156         if (!active)
157                 return;
158
159         x -= dx;
160         y -= dy;
161
162         if (((x >= endX) && (y >= endY)) || ((x <= startX) && (y <= startY)))
163         {
164                 dx = -dx;
165                 dy = -dy;
166                 think = pause;
167                 
168                 if (think == 0)
169                 {
170                         think = 60;
171
172                         if ((x <= startX) && (y <= startY) && (waitAtStart))
173                         {
174                                 active = false;
175                                 think = 0;
176                         }
177
178                         if ((x >= endX) && (y >= endY) && (!waitAtStart))
179                         {
180                                 active = false;
181                                 think = 0;
182                         }
183                 }
184         }
185 }
186
187 int Train::getPause()
188 {
189         return pause;
190 }