]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/obstacles.cpp
Update copyright statements.
[quix0rs-blobwars.git] / src / obstacles.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 "obstacles.h"
23
24 void addObstacle(const char *name, int x, int y, const char *spriteName)
25 {
26         Entity *obstacle = new Entity();
27         
28         obstacle->setName(name);
29         obstacle->place(x, y);
30         obstacle->setSprites(graphics.getSprite(spriteName, true), graphics.getSprite(spriteName, true), graphics.getSprite(spriteName, true));
31         
32         if (map.isIceLevel)
33         {
34                 Math::addBit(&obstacle->flags, ENT_SLIDES);
35         }
36
37         map.addObstacle(obstacle);
38 }
39
40 bool checkObstacleContact(Entity *ent, int dir)
41 {
42         Entity *obstacle = (Entity*)map.obstacleList.getHead();
43         
44         bool collision = false;
45
46         while (obstacle->next != NULL)
47         {
48                 obstacle = (Entity*)obstacle->next;
49                 
50                 if (obstacle->flags & ENT_TELEPORTING)
51                 {
52                         continue;
53                 }
54
55                 if (ent == obstacle)
56                 {
57                         continue;
58                 }
59                         
60                 if (dir == 0)
61                 {
62                         collision = Collision::collision(ent->x + ent->dx, ent->y, ent->width, ent->height - 1, obstacle->x, obstacle->y, obstacle->width, obstacle->height);
63                 }
64                 else
65                 {
66                         collision = Collision::collision(ent->x, ent->y + ent->dy, ent->width, ent->height - 1, obstacle->x, obstacle->y, obstacle->width, obstacle->height);
67                 }
68
69                 if (collision)
70                 {
71                         if (dir == 0)
72                         {
73                                 if ((ent->y + ent->height == obstacle->y + obstacle->height) || ((ent->flags & ENT_BULLET) && (ent->owner == &player)))
74                                 {
75                                         obstacle->dx = (ent->dx / 2);
76                                         
77                                         if (ent->dx < 0) ent->x = obstacle->x + obstacle->width;
78                                         if (ent->dx > 0) ent->x = obstacle->x - ent->width;
79                                 }
80                         }
81
82                         if (dir == 1)
83                         {
84                                 ent->falling = false;
85                                 ent->dy = 0;
86                         }
87
88                         return true;
89                 }
90         }
91
92         return false;
93 }
94
95 void doObstacles()
96 {
97         Entity *obstacle = (Entity*)map.obstacleList.getHead();
98
99         int x, y;
100
101         while (obstacle->next != NULL)
102         {
103                 obstacle = (Entity*)obstacle->next;
104
105                 if (obstacle->flags & ENT_TELEPORTING)
106                 {
107                         moveEntity(obstacle);
108                 }
109                 else
110                 {
111                         x = (int)(obstacle->x - engine.playerPosX);
112                         y = (int)(obstacle->y - engine.playerPosY);
113
114                         // Gravity
115                         if (!(obstacle->flags & ENT_WEIGHTLESS))
116                         {
117                                 obstacle->applyGravity();
118                         }
119
120                         moveEntity(obstacle);
121
122                         if (!(obstacle->flags & ENT_TELEPORTING))
123                         {
124                                 if (!(obstacle->flags & ENT_SLIDES))
125                                 {
126                                         obstacle->dx = 0;
127                                 }
128                         }
129
130                         graphics.blit(obstacle->getFaceImage(), x, y, graphics.screen, false);
131
132                         obstacle->animate();
133                 }
134         }
135 }