Mercurial > molko
comparison libmlk-adventure/adventure/action/teleport.c @ 272:a49ae1b6ea4f
adventure: don't use plural in directories
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 12 Dec 2020 12:16:47 +0100 |
parents | libmlk-adventure/adventure/actions/teleport.c@9bbbabb6f077 |
children | e28429dbdaaf |
comparison
equal
deleted
inserted
replaced
271:eadd3dbfa0af | 272:a49ae1b6ea4f |
---|---|
1 /* | |
2 * teleport.c -- teleport contact | |
3 * | |
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <compat.h> | |
20 | |
21 #include <assert.h> | |
22 #include <stdio.h> | |
23 #include <stdlib.h> | |
24 #include <string.h> | |
25 | |
26 #include <core/action.h> | |
27 #include <core/alloc.h> | |
28 #include <core/maths.h> | |
29 #include <core/painter.h> | |
30 #include <core/panic.h> | |
31 #include <core/texture.h> | |
32 #include <core/window.h> | |
33 | |
34 #include <rpg/map.h> | |
35 | |
36 #include <adventure/molko.h> | |
37 | |
38 #include "teleport.h" | |
39 | |
40 struct teleport { | |
41 struct action action; | |
42 struct texture overlay; | |
43 struct map *map; | |
44 char destination[FILENAME_MAX]; | |
45 unsigned int elapsed; | |
46 unsigned int alpha; | |
47 int origin_x; | |
48 int origin_y; | |
49 int x; | |
50 int y; | |
51 unsigned int w; | |
52 unsigned int h; | |
53 }; | |
54 | |
55 static void | |
56 draw(struct action *act) | |
57 { | |
58 struct teleport *tp = act->data; | |
59 | |
60 texture_set_blend_mode(&tp->overlay, TEXTURE_BLEND_BLEND); | |
61 texture_set_alpha_mod(&tp->overlay, tp->alpha); | |
62 | |
63 PAINTER_BEGIN(&tp->overlay); | |
64 painter_set_color(0x000000ff); | |
65 painter_clear(); | |
66 PAINTER_END(); | |
67 | |
68 texture_draw(&tp->overlay, 0, 0); | |
69 } | |
70 | |
71 static bool | |
72 update_fadeout(struct action *act, unsigned int ticks) | |
73 { | |
74 struct teleport *tp = act->data; | |
75 | |
76 tp->elapsed += ticks; | |
77 | |
78 if (tp->elapsed >= 10) { | |
79 if (tp->alpha >= 255) { | |
80 molko_teleport("assets/maps/map-world.map", tp->origin_x, tp->origin_y); | |
81 return true; | |
82 } | |
83 | |
84 tp->elapsed = 0; | |
85 tp->alpha += 5; | |
86 } | |
87 | |
88 return false; | |
89 } | |
90 | |
91 static bool | |
92 update_touch(struct action *act, unsigned int ticks) | |
93 { | |
94 (void)ticks; | |
95 | |
96 struct teleport *tp = act->data; | |
97 const int x = tp->x - tp->map->player_sprite->cellw; | |
98 const int y = tp->y - tp->map->player_sprite->cellh; | |
99 const unsigned int w = tp->w + tp->map->player_sprite->cellw; | |
100 const unsigned int h = tp->h + tp->map->player_sprite->cellh; | |
101 | |
102 if (maths_is_boxed(x, y, w, h, tp->map->player_x, tp->map->player_y)) { | |
103 /* Stop movement and disable input. */ | |
104 tp->map->player_movement = 0; | |
105 game.inhibit = INHIBIT_STATE_INPUT; | |
106 | |
107 /* | |
108 * We change our update function and add a draw function that | |
109 * fade the screen out. | |
110 */ | |
111 if (!texture_new(&tp->overlay, window.w, window.h)) | |
112 panic(); | |
113 | |
114 act->update = update_fadeout; | |
115 act->draw = draw; | |
116 } | |
117 | |
118 return false; | |
119 } | |
120 | |
121 static void | |
122 finish(struct action *act) | |
123 { | |
124 struct teleport *self = act->data; | |
125 | |
126 texture_finish(&self->overlay); | |
127 | |
128 free(act->data); | |
129 } | |
130 | |
131 struct action * | |
132 teleport_new(struct map *map, | |
133 const char *destination, | |
134 int x, | |
135 int y, | |
136 unsigned int w, | |
137 unsigned int h, | |
138 int origin_x, | |
139 int origin_y) | |
140 { | |
141 assert(map); | |
142 assert(destination); | |
143 | |
144 struct teleport *tp; | |
145 | |
146 tp = alloc_new0(sizeof (*tp)); | |
147 tp->map = map; | |
148 tp->x = x; | |
149 tp->y = y; | |
150 tp->w = w; | |
151 tp->h = h; | |
152 tp->origin_x = origin_x; | |
153 tp->origin_y = origin_y; | |
154 strlcpy(tp->destination, destination, sizeof (tp->destination)); | |
155 | |
156 tp->action.data = tp; | |
157 tp->action.update = update_touch; | |
158 tp->action.finish = finish; | |
159 | |
160 return &tp->action; | |
161 } |