Mercurial > molko
comparison libmlk-adventure/adventure/state/map.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 * map.c -- map state | |
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 <stdio.h> | |
24 #include <string.h> | |
25 | |
26 #include <core/image.h> | |
27 #include <core/painter.h> | |
28 #include <core/panic.h> | |
29 #include <core/state.h> | |
30 | |
31 #include <rpg/map.h> | |
32 #include <rpg/map-file.h> | |
33 | |
34 #include <adventure/actions/teleport.h> | |
35 | |
36 #include "molko.h" | |
37 #include "map.h" | |
38 | |
39 struct self { | |
40 struct state state; | |
41 char name[FILENAME_MAX]; | |
42 int origin_x; | |
43 int origin_y; | |
44 struct map map; | |
45 struct map_file map_file; | |
46 }; | |
47 | |
48 static void | |
49 load_teleport(struct map *map, int x, int y, int w, int h, const char *value) | |
50 { | |
51 char name[128] = {0}; | |
52 int origin_x = -1, origin_y = -1; | |
53 | |
54 sscanf(value, "%127[^|]|%d|%d", name, &origin_x, &origin_y); | |
55 action_stack_add(&map->astack_par, teleport_new(map, name, x, y, w, h, origin_x, origin_y)); | |
56 } | |
57 | |
58 static void | |
59 load_action(struct map *map, int x, int y, int w, int h, const char *value) | |
60 { | |
61 static const struct { | |
62 const char *name; | |
63 void (*load)(struct map *, int, int, int, int, const char *); | |
64 } table[] = { | |
65 { "teleport|", load_teleport } | |
66 }; | |
67 | |
68 for (size_t i = 0; i < NELEM(table); ++i) { | |
69 size_t len = strlen(table[i].name); | |
70 | |
71 if (strncmp(table[i].name, value, len) == 0) { | |
72 table[i].load(map, x, y, w, h, value + len); | |
73 break; | |
74 } | |
75 } | |
76 } | |
77 | |
78 static void | |
79 start(struct state *state) | |
80 { | |
81 struct self *self = state->data; | |
82 | |
83 self->map_file.load_action = load_action; | |
84 | |
85 if (!map_file_open(&self->map_file, &self->map, molko_path(self->name))) | |
86 panic(); | |
87 | |
88 /* TODO: find this from team maybe. */ | |
89 if (!image_open(&molko.map_player_texture, molko_path("assets/sprites/john.png"))) | |
90 panic(); | |
91 | |
92 sprite_init(&molko.map_player_sprite, &molko.map_player_texture, 48, 48); | |
93 self->map.player_sprite = &molko.map_player_sprite; | |
94 | |
95 /* TODO: add support for saving origin in save. */ | |
96 if (self->origin_x >= 0) | |
97 self->map.player_x = self->origin_x; | |
98 if (self->origin_y >= 0) | |
99 self->map.player_y = self->origin_y; | |
100 | |
101 if (!map_init(&self->map)) | |
102 panic(); | |
103 } | |
104 | |
105 static void | |
106 handle(struct state *state, const union event *ev) | |
107 { | |
108 struct self *self = state->data; | |
109 | |
110 map_handle(&self->map, ev); | |
111 } | |
112 | |
113 static void | |
114 update(struct state *state, unsigned int ticks) | |
115 { | |
116 struct self *self = state->data; | |
117 | |
118 map_update(&self->map, ticks); | |
119 } | |
120 | |
121 static void | |
122 draw(struct state *state) | |
123 { | |
124 struct self *self = state->data; | |
125 | |
126 painter_clear(); | |
127 map_draw(&self->map); | |
128 painter_present(); | |
129 } | |
130 | |
131 static void | |
132 finish(struct state *state) | |
133 { | |
134 struct self *self = state->data; | |
135 | |
136 map_finish(&self->map); | |
137 map_file_finish(&self->map_file); | |
138 | |
139 free(self); | |
140 } | |
141 | |
142 struct state * | |
143 map_state_new(const char *name, int origin_x, int origin_y) | |
144 { | |
145 (void)origin_x; | |
146 (void)origin_y; | |
147 | |
148 struct self *self; | |
149 | |
150 self = alloc_new0(sizeof (*self)); | |
151 self->origin_x = origin_x; | |
152 self->origin_y = origin_y; | |
153 strlcpy(self->name, name, sizeof (self->name)); | |
154 | |
155 self->state.data = self; | |
156 self->state.start = start; | |
157 self->state.handle = handle; | |
158 self->state.update = update; | |
159 self->state.draw = draw; | |
160 self->state.finish = finish; | |
161 | |
162 return &self->state; | |
163 } |