]> git.mxchange.org Git - quix0rs-blobwars.git/blob - src/obstacles.cpp
Added .gitignore to ignore certain files + fixed access rights on Makefile* as
[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 == DIR_X)
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 if (dir == DIR_Y)
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                 else
69                 {
70                         collision = Collision::collision(ent->x + ent->dx, ent->y + ent->dy, ent->width, ent->height - 1, obstacle->x, obstacle->y, obstacle->width, obstacle->height);
71                 }
72
73                 if (collision)
74                 {
75                         if (dir & DIR_X)
76                         {
77                                 if ((ent->y + ent->height == obstacle->y + obstacle->height) || ((ent->flags & ENT_BULLET) && (ent->owner == &player)))
78                                 {
79                                         obstacle->dx = (ent->dx / 2);
80                                         
81                                         if (ent->dx < 0) ent->x = obstacle->x + obstacle->width;
82                                         if (ent->dx > 0) ent->x = obstacle->x - ent->width;
83                                 }
84                         }
85
86                         if (dir & DIR_Y)
87                         {
88                                 ent->falling = false;
89                                 ent->dy = 0;
90                         }
91
92                         return true;
93                 }
94         }
95
96         return false;
97 }
98
99 void doObstacles()
100 {
101         Entity *obstacle = (Entity*)map.obstacleList.getHead();
102
103         int x, y;
104
105         while (obstacle->next != NULL)
106         {
107                 obstacle = (Entity*)obstacle->next;
108
109                 if (obstacle->flags & ENT_TELEPORTING)
110                 {
111                         moveEntity(obstacle);
112                 }
113                 else
114                 {
115                         x = (int)(obstacle->x - engine.playerPosX);
116                         y = (int)(obstacle->y - engine.playerPosY);
117
118                         // Gravity
119                         if (!(obstacle->flags & ENT_WEIGHTLESS))
120                         {
121                                 obstacle->applyGravity();
122                         }
123
124                         moveEntity(obstacle);
125
126                         if (!(obstacle->flags & ENT_TELEPORTING))
127                         {
128                                 if (!(obstacle->flags & ENT_SLIDES))
129                                 {
130                                         obstacle->dx = 0;
131                                 }
132                         }
133
134                         graphics.blit(obstacle->getFaceImage(), x, y, graphics.screen, false);
135
136                         obstacle->animate();
137                 }
138         }
139 }