1 /*
2 Copyright (C) 2009-2021 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, 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 */
19 
20 #include "../headers.h"
21 
22 #include "../collisions.h"
23 #include "../custom_actions.h"
24 #include "../entity.h"
25 #include "../graphics/animation.h"
26 #include "../system/error.h"
27 #include "../system/properties.h"
28 
29 extern Entity *self;
30 
31 static void entityWait(void);
32 static void touch(Entity *);
33 
addAntiGravityField(int x,int y,char * name)34 Entity *addAntiGravityField(int x, int y, char *name)
35 {
36 	Entity *e = getFreeEntity();
37 
38 	if (e == NULL)
39 	{
40 		showErrorAndExit("No free slots to add an Anti Gravity Field");
41 	}
42 
43 	loadProperties(name, e);
44 
45 	e->x = x;
46 	e->y = y;
47 
48 	e->type = ANTI_GRAVITY;
49 
50 	e->face = RIGHT;
51 
52 	e->action = &entityWait;
53 	e->touch = &touch;
54 
55 	e->draw = &drawLoopingAnimationToMap;
56 
57 	setEntityAnimation(e, "STAND");
58 
59 	return e;
60 }
61 
entityWait()62 static void entityWait()
63 {
64 	if (self->active == TRUE)
65 	{
66 		self->flags &= ~NO_DRAW;
67 	}
68 
69 	else
70 	{
71 		self->flags |= NO_DRAW;
72 	}
73 }
74 
touch(Entity * other)75 static void touch(Entity *other)
76 {
77 	int bottomBefore;
78 
79 	if (self->active == TRUE && !(other->flags & FLY))
80 	{
81 		if (other->dirY > 0)
82 		{
83 			bottomBefore = other->y + other->h - other->dirY - 1;
84 
85 			if (bottomBefore < self->y)
86 			{
87 				/* Place the player as close to the solid tile as possible */
88 
89 				other->y = self->y;
90 				other->y -= other->h;
91 
92 				other->standingOn = self;
93 				other->dirY = 0;
94 				other->flags |= ON_GROUND;
95 			}
96 
97 			else
98 			{
99 				setCustomAction(other, &antiGravity, 2, 0, 1);
100 			}
101 		}
102 
103 		else
104 		{
105 			setCustomAction(other, &antiGravity, 2, 0, 1);
106 		}
107 	}
108 }
109