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