This repository has been archived by the owner on Feb 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.c
96 lines (79 loc) · 2.26 KB
/
world.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* SPDX-License-Identifier: GPL-2.0-or-later
* Copyright (C) 2023 Andy Frank Schoknecht
*/
#include <stdint.h>
#include <stdio.h>
#include "engine/log.h"
#include "entity.h"
#include "path.h"
#include "world.h"
struct World Ch_World_new(const size_t width, const size_t height)
{
struct World world = World_new(BLOCK_SIZE, width, height, 2);
// set values
world.entities[0].id = E_PLAYER;
world.entities[0].rect.x = 0.0f;
world.entities[0].rect.y = 0.0f;
world.entities[0].rect.w = DATA_ENTITIES[E_PLAYER].width;
world.entities[0].rect.h = DATA_ENTITIES[E_PLAYER].height;
world.entities[0].grounded = 0;
world.entities[0].velocity_x = 0.0f;
world.entities[0].velocity_y = 0.0f;
return world;
}
struct World Ch_World_from_file(const char *world_name)
{
const char *temp;
struct World world;
struct String filepath = String_new(8);
// get path
if (get_world_path(&filepath) != 0) {
world.invalid = 1;
return world;
}
String_append(&filepath, world_name, strlen(world_name));
temp = ".";
String_append(&filepath, temp, strlen(temp));
String_append(&filepath, FILETYPE_WORLD, strlen(FILETYPE_WORLD));
// read
world = World_from_file(filepath.str);
if (world.invalid) {
struct String msg = String_new(16);
temp = "World \"";
String_copy(&msg, temp, strlen(temp));
String_append(&msg, world_name, strlen(world_name));
temp = "\" could not be read.";
String_append(&msg, temp, strlen(temp));
log_err(msg.str);
String_clear(&msg);
}
String_clear(&filepath);
return world;
}
void Ch_World_to_file(struct World *world, const char *world_name)
{
const char *temp;
struct String filepath = String_new(8);
// get path
if (get_world_path(&filepath) != 0) {
world->invalid = 1;
return;
}
String_append(&filepath, world_name, strlen(world_name));
temp = ".";
String_append(&filepath, temp, strlen(temp));
String_append(&filepath, FILETYPE_WORLD, strlen(FILETYPE_WORLD));
// write
Ch_World_to_file(world, filepath.str);
if (world->invalid) {
struct String msg = String_new(16);
temp = "World \"";
String_copy(&msg, temp, strlen(temp));
String_append(&msg, world_name, strlen(world_name));
temp = "\" could not be read.";
String_append(&msg, temp, strlen(temp));
log_err(msg.str);
String_clear(&msg);
}
String_clear(&filepath);
}