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 "../audio/music.h"
23 #include "../custom_actions.h"
24 #include "../entity.h"
25 #include "../event/script.h"
26 #include "../game.h"
27 #include "../graphics/animation.h"
28 #include "../hud.h"
29 #include "../system/error.h"
30 #include "../system/properties.h"
31 
32 extern Entity *self, player, playerShield, playerWeapon;
33 extern Game game;
34 
35 static void entityWait(void);
36 static void touch(Entity *);
37 static void activate(int);
38 
addLevelExit(char * name,int x,int y)39 Entity *addLevelExit(char *name, int x, int y)
40 {
41 	Entity *e = getFreeEntity();
42 
43 	if (e == NULL)
44 	{
45 		showErrorAndExit("No free slots to add the Level Exit");
46 	}
47 
48 	loadProperties("common/level_exit", e);
49 
50 	e->x = x;
51 	e->y = y;
52 
53 	e->endX = x;
54 
55 	STRNCPY(e->name, name, sizeof(e->name));
56 
57 	e->action = &entityWait;
58 
59 	e->draw = &drawLoopingAnimationToMap;
60 	e->touch = &touch;
61 	e->activate = &activate;
62 	e->thinkTime = 60;
63 
64 	e->type = LEVEL_EXIT;
65 
66 	setEntityAnimation(e, "STAND");
67 
68 	return e;
69 }
70 
entityWait()71 static void entityWait()
72 {
73 	self->dirX = (self->face == RIGHT ? 10 : -10);
74 
75 	self->thinkTime--;
76 
77 	if (self->thinkTime <= 0)
78 	{
79 		self->thinkTime = 60;
80 
81 		self->x = self->startX;
82 	}
83 
84 	else if (self->thinkTime % 20 == 0)
85 	{
86 		self->x += self->dirX;
87 	}
88 
89 	if (self->active == FALSE)
90 	{
91 		self->flags |= NO_DRAW;
92 	}
93 
94 	else
95 	{
96 		self->flags &= ~NO_DRAW;
97 	}
98 }
99 
touch(Entity * other)100 static void touch(Entity *other)
101 {
102 	if (other->type == PLAYER && self->active == TRUE)
103 	{
104 		setInfoBoxMessage(0, 255, 255, 255, _("Press Action to go to %s"), _(self->requires));
105 	}
106 }
107 
activate(int val)108 static void activate(int val)
109 {
110 	if (self->active == TRUE)
111 	{
112 		if (game.mapExitable > 0)
113 		{
114 			runScript("items_missing");
115 
116 			return;
117 		}
118 
119 		player.flags |= HELPLESS;
120 
121 		setCustomAction(&player, &helpless, 600, 0, 0);
122 		setCustomAction(&player, &invulnerableNoFlash, 600, 0, 0);
123 
124 		setEntityAnimation(&player, "STAND");
125 		setEntityAnimation(&playerWeapon, "STAND");
126 		setEntityAnimation(&playerShield, "STAND");
127 
128 		player.dirX = 0;
129 
130 		setNextLevel(self->name, self->objectiveName);
131 
132 		setTransition(TRANSITION_OUT, &goToNextMap);
133 
134 		fadeOutMusic(1000);
135 	}
136 }
137