-
Notifications
You must be signed in to change notification settings - Fork 2
/
CelesteUtils.h
153 lines (143 loc) · 4.94 KB
/
CelesteUtils.h
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//
// Created by gonengazit on 01/07/2020.
//
#ifndef CPPLESTE_CELESTEUTILS_H
#define CPPLESTE_CELESTEUTILS_H
#include "PICO8.h"
#include <map>
#include <string>
#include <algorithm>
#include <iostream>
#include <chrono>
#include <thread>
#include <vector>
namespace utils {
template<typename Cart>
void enable_loop_mode(PICO8<Cart> &p8){
p8.game().loop_mode=true;
}
template<typename Cart>
void set_max_djump(PICO8<Cart> &p8, int max_djump) {
p8.game().max_djump = max_djump;
}
template<typename Cart>
void load_room(PICO8<Cart> &p8, int level_id, bool loading_jank = false) {
p8.set_btn_state(0);
p8.game().load_room(level_id % 8, level_id / 8);
if (loading_jank && level_id > 0) {
int object_counts[] = {2, 1, 4, 14, 3, 2, 12, 9, 6, 5, 7, 3, 6, 5, 11, 8, 4, 7, 3, 6, 8, 2, 2, 1, 8, 3, 3,
7, 6,
7, 2};
int cnt = 0;
int start = object_counts[level_id - 1] - 1;
int end = object_counts[level_id];
for (auto &o:p8.game().objects) {
if (cnt >= end) {
break;
}
if (cnt < start) {
continue;
}
o->move(o->spd.x, o->spd.y);
o->update();
}
}
}
template<typename obj, typename Cart>
void supress_object(PICO8<Cart> &p8) {
auto &objects = p8.game().objects;
auto it = objects.begin();
while (it != objects.end()) {
auto nxt = next(it);
if (it->get() && it->get()->type_id == obj::type_enum) {
objects.erase(it);
}
it = nxt;
}
}
template<typename Cart>
void skip_player_spawn(PICO8<Cart> &p8) {
typename Cart::base_obj *p;
while (true) {
p=p8.game().get_player();
if(p && p->type_id==Cart::player::type_enum){
return;
}
else if(p && p->type_id==Cart::player_spawn::type_enum){
auto q=static_cast<typename Cart::player_spawn*>(p);
if (q->state==2 && q->delay<=0){
q->update();
//clear the destroyed player spawn obj
for (auto it=p8.game().objects.begin(); it!=p8.game().objects.end();){
if(*it==nullptr){
it=p8.game().objects.erase(it);
}
else{
it++;
}
}
return;
}
}
p8.step();
}
}
template<typename Cart>
void replace_room(PICO8<Cart> &p8, int level_id, std::string room_data) {
std::map<char, int> tiles = {
{'w', 32}, // terrain
{'^', 17}, // up spike
{'v', 27}, // down spike
{'<', 59}, // left spike
{'>', 43}, // right spike
{'b', 22}, // balloon
{'c', 23}, // crumble block
{'s', 18}, // spring
{'p', 1}, // player spawn
{'.', 0} // empty
};
room_data.erase(std::remove_if(room_data.begin(),room_data.end(),isspace),room_data.end());
int rx = level_id % 8;
int ry = level_id / 8;
for (int tx = 0; tx < 16; tx++) {
for (int ty = 0; ty < 16; ty++) {
char tile = room_data[tx + 16 * ty];
p8.mset(rx * 16 + tx, ry * 16 + ty, tiles[tile]); // if tile doesn't exist tiles[tile] will return 0
}
}
}
template<typename Cart>
void
place_maddy(PICO8<Cart> &p8, int x, int y, double remx=0, double remy=0, double spdx=0, double spdy=0, int grace=6,
int djump=1) {
auto *p = p8.game().get_player();
if(p!= nullptr) {
for (auto it = p8.game().objects.begin(); it != p8.game().objects.end(); it++) {
if (p == it->get()) {
p8.game().objects.erase(it);
break;
}
}
}
auto &p2 = dynamic_cast<typename Cart::player &>(p8.game().template init_object<typename Cart::player>(x, y));
p2.rem.x = remx;
p2.rem.y = remy;
p2.spd.x = spdx;
p2.spd.y = spdy;
p2.grace = grace;
p2.djump = djump;
}
template<typename Cart>
void watch_inputs(PICO8<Cart> &p8, const std::vector<int>& inputs){
std::cout<<p8.input_display()<<std::endl;
std::cout<<p8.game()<<std::endl;
for(auto a:inputs) {
std::this_thread::sleep_for(std::chrono::milliseconds(1000/30));
p8.set_btn_state(a);
p8.step();
std::cout<<p8.input_display()<<std::endl;
std::cout<<p8.game()<<std::endl;
}
}
}
#endif //CPPLESTE_CELESTEUTILS_H