Mirai's Miscellaneous Misadventures

M43 / chapters / chapter-I / setup.c

1/* license: AGPLv3 or later */
2/* copyright 2023 zamfofex */
3
4#include <mimimi-chapters.h>
5
6#include "ground.c"
7#include "background.c"
8
9static void mimimi_chapter_I_video_set(struct mimimi_test_video_set *tset, struct mimimi_video_set *set)
10{
11	static int video_count = 8;
12	static int image_count = 8;
13	static int width = 48;
14	static int height = 48;
15	
16	int i, j, k;
17	struct mimimi_video *videos, *standing, *knocked, *falling, *jumping;
18	struct mimimi_image *images, *image;
19	unsigned char *colors;
20	void **textures;
21	
22	videos = tset->videos;
23	images = tset->images;
24	colors = tset->colors;
25	textures = tset->textures;
26	
27	set->standing = videos;
28	set->standing_video_count = video_count;
29	videos += video_count;
30	
31	set->jumping = videos;
32	set->jumping_video_count = video_count;
33	videos += video_count;
34	
35	for (i = 0 ; i < video_count ; i++)
36	{
37		standing = set->standing + i;
38		standing->count = image_count;
39		standing->images = images;
40		images += image_count;
41		standing->textures = textures;
42		textures += image_count;
43		
44		for (j = 0 ; j < image_count ; j++)
45		{
46			image = standing->images + j;
47			image->width = width;
48			image->height = height;
49			image->colors = colors;
50			colors += width * height;
51			for (k = 0 ; k < width * height ; k++) image->colors[k] = 0;
52		}
53		
54		jumping = set->jumping + i;
55		jumping->count = image_count;
56		jumping->images = images;
57		images += image_count;
58		jumping->textures = textures;
59		textures += image_count;
60		
61		for (j = 0 ; j < image_count ; j++)
62		{
63			image = jumping->images + j;
64			image->width = width;
65			image->height = height;
66			image->colors = colors;
67			colors += width * height;
68			for (k = 0 ; k < width * height ; k++) image->colors[k] = 0;
69		}
70	}
71	
72	knocked = videos++;
73	knocked->count = 1;
74	knocked->images = images++;
75	knocked->images[0].width = width;
76	knocked->images[0].height = height;
77	knocked->images[0].colors = colors;
78	knocked->textures = textures++;
79	colors += width * height;
80	for (i = 0 ; i < width * height ; i++) knocked->images[0].colors[i] = 0;
81	
82	falling = videos++;
83	falling->count = 1;
84	falling->images = images++;
85	falling->images[0].width = width;
86	falling->images[0].height = height;
87	falling->images[0].colors = colors;
88	colors += width * height;
89	falling->textures = textures++;
90	for (i = 0 ; i < width * height ; i++) falling->images[0].colors[i] = 0;
91	
92	set->knocked = knocked;
93	set->falling = falling;
94}
95
96static void mimimi_chapter_I_appearance(struct mimimi_test_sprite *sprite, struct mimimi_engine *engine)
97{
98	mimimi_chapter_I_video_set(&sprite->left, &sprite->appearance.left);
99	mimimi_chapter_I_video_set(&sprite->right, &sprite->appearance.right);
100	
101	mimimi_appearance(&sprite->appearance, sprite->model, 24, 37);
102	mimimi_prepare_appearance(&sprite->appearance, engine);
103}
104
105static void mimimi_chapter_I_origin(struct mimimi_ground *ground, struct mimimi_position *position)
106{
107	for (position->y = 0 ; position->y < ground->height ; position->y++)
108	for (position->x = 0 ; position->x < ground->width ; position->x++)
109	{
110		if (ground->tiles[position->x + position->y * ground->width] == 2)
111			return;
112	}
113}
114
115
116static void mimimi_chapter_I_sprite(struct mimimi_test_sprite *sprite, struct mimimi_model *model, struct mimimi_chapter_I *chapter)
117{
118	sprite->model = model;
119	sprite->display.camera = &chapter->clamped_camera.output;
120	sprite->display.sprite = &sprite->sprite;
121	sprite->display.appearance = &sprite->appearance;
122	sprite->display.animation_time = 0;
123	sprite->display.direction = 1;
124	sprite->display.width = 24;
125	sprite->display.height = 48;
126	mimimi_sprite(&sprite->sprite, mimimi_chapter_I_ground, chapter->origin.x * 128, chapter->origin.y * 128, 80, 250);
127	mimimi_chapter_I_appearance(sprite, chapter->engine);
128}
129
130static void mimimi_chapter_I_player_tick(struct mimimi_chapter_I *chapter)
131{
132	mimimi_controls_tick(&chapter->player.sprite.walk, &chapter->history, &mimimi_jump, &chapter->player.sprite);
133	mimimi_sprite_tick(&chapter->player.sprite);
134}
135
136static void mimimi_chapter_I_unpack_bits(int size, unsigned char *unpacked, unsigned char *packed)
137{
138	int i, j, k, n;
139	
140	i = 0;
141	j = 0;
142	
143	while (j < size)
144	{
145		n = packed[i++];
146		if (n == 128) continue;
147		if (n > 128) n -= 256;
148		
149		if (n > 0)
150		{
151			for (k = 0 ; k <= n ; k++)
152				unpacked[j++] = packed[i++];
153		}
154		else
155		{
156			for (k = 0 ; k <= -n ; k++)
157				unpacked[j++] = packed[i];
158			i++;
159		}
160	}
161}
162
163static void mimimi_chapter_I_background(struct mimimi_chapter_I *chapter)
164{
165	int size;
166	
167	chapter->background_image.colors = chapter->background_colors;
168	chapter->background_image.width = mimimi_chapter_I_background_width;
169	chapter->background_image.height = mimimi_chapter_I_background_height;
170	size = chapter->background_image.width * chapter->background_image.height;
171	
172	mimimi_chapter_I_unpack_bits(size, chapter->background_colors, mimimi_chapter_I_background_packed);
173	
174	chapter->background.texture = (*chapter->engine->texture)(chapter->engine->data, &chapter->background_image);
175	chapter->background.camera = &chapter->clamped_camera.output;
176	chapter->background.x = 5 * 8 - 256;
177	chapter->background.y = 0;
178	chapter->background.z = 0;
179}
180
181static void mimimi_chapter_I_camera(struct mimimi_chapter_I *chapter)
182{
183	static struct mimimi_clamped_camera_threshold thresholds[] =
184	{
185		{-35, 0, 0},
186		{8, 2, 2},
187	};
188	
189	chapter->clamped_camera.origin = &chapter->origin;
190	chapter->clamped_camera.position = &chapter->player.sprite.position;
191	chapter->clamped_camera.input = &chapter->camera;
192	chapter->clamped_camera.thresholds = thresholds;
193	chapter->clamped_camera.count = sizeof thresholds / sizeof *thresholds;
194}
195
196void mimimi_chapter_I_tick(void *data, unsigned char left, unsigned char right)
197{
198	struct mimimi_chapter_I *chapter;
199	chapter = data;
200	mimimi_history_tick(&chapter->history, left, right);
201	mimimi_chapter_I_player_tick(chapter);
202	mimimi_background_tick(&chapter->background, chapter->engine);
203	mimimi_display_tick(&chapter->player.display, chapter->engine);
204	mimimi_camera_tick(&chapter->camera, &chapter->player.sprite.position, 0, -512);
205	mimimi_clamped_camera_tick(&chapter->clamped_camera);
206	if (chapter->progression == 0)
207	{
208		if (chapter->player.sprite.position.x < 1024)
209			chapter->progression++;
210	}
211	if (chapter->progression == 1)
212	{
213		if (mimimi_toast_dialogue_bottom_tick(&chapter->dialogue) != 0)
214			chapter->progression++;
215	}
216}
217
218void mimimi_chapter_I(void *data, struct mimimi_engine *engine)
219{
220	struct mimimi_dialogue_paragraph paragraphs[] =
221	{
222		{0, "Mango", "Hello, world!"},
223	};
224	
225	struct mimimi_dialogue dialogue;
226	struct mimimi_chapter_I *chapter;
227	
228	paragraphs[0].model = mimimi_mango;
229	
230	chapter = data;
231	chapter->engine = engine;
232	
233	chapter->progression = 0;
234	dialogue.count = sizeof paragraphs / sizeof *paragraphs;
235	dialogue.paragraphs = paragraphs;
236	mimimi_toast_dialogue(&chapter->dialogue, &dialogue, engine);
237	
238	mimimi_chapter_I_origin(mimimi_chapter_I_ground, &chapter->origin);
239	mimimi_chapter_I_sprite(&chapter->player, mimimi_mango, chapter);
240	
241	chapter->history.left = 0;
242	chapter->history.right = 0;
243	
244	chapter->camera = chapter->player.sprite.position;
245	chapter->camera.y -= 512;
246	
247	mimimi_chapter_I_camera(chapter);
248	mimimi_chapter_I_background(chapter);
249	
250	mimimi_clamped_camera_tick(&chapter->clamped_camera);
251}