comparison libmlk-adventure/adventure/actions/teleport.c @ 259:16be1ad3ddba

adventure: start working on maps and teleport
author David Demelier <markand@malikania.fr>
date Sun, 06 Dec 2020 11:22:03 +0100
parents
children 60a214ec1ab4
comparison
equal deleted inserted replaced
258:f978fa0137ce 259:16be1ad3ddba
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 <stdlib.h>
23 #include <string.h>
24
25 #include <core/alloc.h>
26 #include <core/maths.h>
27 #include <core/painter.h>
28 #include <core/panic.h>
29 #include <core/texture.h>
30 #include <core/window.h>
31
32 #include <rpg/map.h>
33
34 #include "molko.h"
35 #include "teleport.h"
36
37 static void
38 draw(struct action *act)
39 {
40 struct teleport *tp = act->data;
41
42 texture_set_blend_mode(&tp->overlay, TEXTURE_BLEND_BLEND);
43 texture_set_alpha_mod(&tp->overlay, tp->alpha);
44
45 PAINTER_BEGIN(&tp->overlay);
46 painter_set_color(0x000000ff);
47 painter_clear();
48 PAINTER_END();
49
50 texture_draw(&tp->overlay, 0, 0);
51 }
52
53 static bool
54 update_fadeout(struct action *act, unsigned int ticks)
55 {
56 struct teleport *tp = act->data;
57
58 tp->elapsed += ticks;
59
60 if (tp->elapsed >= 10) {
61 if (tp->alpha >= 255) {
62 molko_teleport("assets/maps/map-world.map", tp->origin_x, tp->origin_y);
63 return true;
64 }
65
66 tp->elapsed = 0;
67 tp->alpha += 5;
68 }
69
70 return false;
71 }
72
73 static bool
74 update_touch(struct action *act, unsigned int ticks)
75 {
76 (void)ticks;
77
78 struct teleport *tp = act->data;
79 const int x = tp->x - tp->map->player_sprite->cellw;
80 const int y = tp->y - tp->map->player_sprite->cellh;
81 const unsigned int w = tp->w + tp->map->player_sprite->cellw;
82 const unsigned int h = tp->h + tp->map->player_sprite->cellh;
83
84 if (maths_is_boxed(x, y, w, h, tp->map->player_x, tp->map->player_y)) {
85 /* Stop movement and disable input. */
86 tp->map->player_movement = 0;
87 game.inhibit = INHIBIT_STATE_INPUT;
88
89 /*
90 * We change our update function and add a draw function that
91 * fade the screen out.
92 */
93 if (!texture_new(&tp->overlay, window.w, window.h))
94 panic();
95
96 act->update = update_fadeout;
97 act->draw = draw;
98 }
99
100 return false;
101 }
102
103 static void
104 finish(struct action *act)
105 {
106 struct teleport *self = act->data;
107
108 texture_finish(&self->overlay);
109
110 free(act->data);
111 }
112
113 struct action *
114 teleport_new(struct map *map,
115 const char *destination,
116 int x,
117 int y,
118 unsigned int w,
119 unsigned int h,
120 int origin_x,
121 int origin_y)
122 {
123 assert(map);
124 assert(destination);
125
126 struct teleport *tp;
127
128 tp = alloc_new0(sizeof (*tp));
129 tp->map = map;
130 tp->x = x;
131 tp->y = y;
132 tp->w = w;
133 tp->h = h;
134 tp->origin_x = origin_x;
135 tp->origin_y = origin_y;
136 strlcpy(tp->destination, destination, sizeof (tp->destination));
137
138 tp->action.data = tp;
139 tp->action.update = update_touch;
140 tp->action.finish = finish;
141
142 return &tp->action;
143 }