treesummaryrefslogcommitdiff
path: root/src/main.cpp
blob: 888035d5eef6f39cbcce52cf640aa0c7677a4aea (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include <fstream>
#include <stdio.h>
#include <string>
#include <time.h>
#include <windows.h>
#include <Richedit.h>

#pragma comment(lib, "user32.lib")

#define CONSOLE

#ifdef CONSOLE
#pragma comment(linker, "/subsystem:console")
#else
#pragma comment(linker, "/subsystem:windows")
#endif

using namespace std;


const int WIDTH = 56, HEIGHT = 29;

std::string map;

clock_t game_clock;
double frame_time = 30;

int lvl = 0;

// Util

double get_dur(clock_t then) {
  double result = (double)(clock() - then) / CLOCKS_PER_SEC;
  return result * 1000;
}

// Edit

HWND hwnd_notepad = NULL;
HWND hwnd_edit = NULL;

string get_text() {
  int len = SendMessageA(hwnd_edit, WM_GETTEXTLENGTH, 0, 0);
  char *buffer = new char[len + 1];
  int read = SendMessageA(hwnd_edit, WM_GETTEXT, len + 1, (LPARAM)buffer);
  if (read != len)
    puts("???");
  buffer[read] = 0;
  string result(buffer);
  delete[] buffer;
  return result;
}

void select_all() {
  SendMessage(hwnd_edit, EM_SETSEL, 0, get_text().size());
}

void select_length(int from, int length) {
  SendMessage(hwnd_edit, EM_SETSEL, from, from + length);
}

void select_from_to(int from, int to) {
  SendMessage(hwnd_edit, EM_SETSEL, from, to);
}

void replace(string str) {
  SendMessage(hwnd_edit, EM_REPLACESEL, FALSE, (LPARAM)str.c_str());
}

int get_pos(int x, int y) {
  return (y + 1) * (WIDTH + 4) + x + 1;
}

char get_block(int x, int y) {
  return map[get_pos(x, y)];
}

// Player
struct Player {
  bool left = false;
  int x, y;
  int x_spawn, y_spawn;
  int jumping = 0;

  void clear() {
    int pos = get_pos(x, y);
    select_length(pos, 1);
    replace({ map[pos], 0 });
  }
  
  void draw() {
    int pos = get_pos(x, y);
    select_length(pos, 1);
    replace({ left ? '<' : '>', 0 });
  }

  void move_to(int x, int y) {
    clear();
    if (this->x > x) left = true;
    if (this->x < x) left = false;
    this->x = x;
    this->y = y;
    draw();
  }

  void move(int dx, int dy) {
    move_to(x + dx, y + dy);
  }

  bool collision_x(int n) {
    if (x + n < 0 || x + n >= WIDTH)
      return true;
    for (int i = 0; i != n; i += (n < 0 ? -1 : 1))
      if (get_block(x + i + (n < 0 ? -1 : 1), y) == 'X')
        return true;
    return false;
  }
  bool collision_y(int n) {
    if (y + n < 0 || y + n >= HEIGHT)
      return true;
    for (int i = 0; i != n; i += (n < 0 ? -1 : 1))
      if (get_block(x, y + i + (n < 0 ? -1 : 1)) == 'X')
        return true;
    return false;
  }
};
Player player { 0, 0 };

// Lvl

std::string read_map(int lvl) {
  std::ifstream ifs("lvl/" + std::to_string(lvl) + ".txt",
                    std::ios::in | std::ios::binary);
  if (!ifs.good())
    puts("mist");
  ifs.seekg(0, std::ios::end);
  int length = ifs.tellg();
  ifs.seekg(0, std::ios::beg);
  char *buffer = new char[length + 1];
  ifs.read(buffer, length);
  ifs.close();
  buffer[length] = 0;
  std::string result(buffer);
  delete buffer;
  return result;
}

void redraw() {
  select_all();
  replace(map);
  player.draw();
}

void load_level(int l) {
  lvl = l;
  map = read_map(l);
  redraw();
  for (int x = 0; x < WIDTH; x++) {
    for (int y = 0; y < HEIGHT; y++) {
      if (map[get_pos(x, y)] == 'S') {
        player.x_spawn = x;
        player.y_spawn = y;
        player.move_to(x, y);
        return;
      }
    }
  }
}

// Notepad

STARTUPINFOA si;
PROCESS_INFORMATION pi;

BOOL CALLBACK ew_cb(HWND hwnd, LPARAM lp) {
  DWORD pid;
  DWORD tid = GetWindowThreadProcessId(hwnd, &pid);
  if (pi.dwProcessId == pid && pi.dwThreadId == tid) {
    hwnd_notepad = hwnd;
    return false;
  }
  return true;
}
BOOL CALLBACK ecw_cb(HWND child, LPARAM in) {
  char buffer[1024 + 1];
  int len = GetClassNameA(child, buffer, 1024);
  buffer[len] = 0;
  if (strcmp(buffer, "Edit") == 0) {
    hwnd_edit = child;
    return false;
  }
  return true;
}

void start_notepad() {
  ZeroMemory(&si, sizeof(si));
  si.cb = sizeof(si);
  ZeroMemory(&pi, sizeof(pi));

  if (!CreateProcessA(NULL,  // No module name (use command line)
                      "notepad.exe",   // Command line
                      NULL,  // Process handle not inheritable
                      NULL,  // Thread handle not inheritable
                      FALSE, // Set handle inheritance to FALSE
                      0,     // No creation flags
                      NULL,  // Use parent's environment block
                      NULL,  // Use parent's starting directory
                      &si,   // Pointer to STARTUPINFO structure
                      &pi)   // Pointer to PROCESS_INFORMATION structure
  ) {
    printf("CreateProcess failed (%d).\n", GetLastError());
  }
  Sleep(100);
  EnumWindows(ew_cb, 0);
  EnumChildWindows(hwnd_notepad, ecw_cb, 0);
  SendMessage(hwnd_edit, EM_SETREADONLY, TRUE, NULL);
}
void close_notepad() {
  TerminateProcess(pi.hProcess, 0);
  CloseHandle(pi.hProcess);
  CloseHandle(pi.hThread);
}

// Keys

enum class Key {
  Left,
  Right,
  Jump,
  Exit,
  Redraw,
  COUNT
};

bool key_state[(uint64_t)Key::COUNT];
bool key_state_old[(uint64_t)Key::COUNT];

int key_get_vk(Key key) {
  switch (key) {
    case Key::Left: return 'A';
    case Key::Right: return 'D';
    case Key::Jump: return VK_SPACE;
    case Key::Exit: return VK_ESCAPE;
    case Key::Redraw: return 'R';
    default: return 0;
  }
}

void update_key_state() {
  for (int i = 0; i < (int)Key::COUNT; i++) {
    key_state[i] = GetAsyncKeyState(key_get_vk((Key)i));
  }
}

void update_key_state_old() {
  for (int i = 0; i < (int)Key::COUNT; i++) {
    key_state_old[i] = key_state[i];
  }
}

bool key_pressed(Key key) {
  return key_state[(int)key] && !key_state_old[(int)key];
}

bool key_down(Key key) {
  return key_state[(int)key];
}

bool key_up(Key key) {
  return !key_state[(int)key];
}

// Gameplay
  
clock_t jump_clock = clock();
int jump_height = 3;
double jump_time1 = 50;
double jump_time2 = 100;
int text_speed = 50;

void update_play(bool can_jump = true, int x_min = 0, int x_max = WIDTH - 1) {
  if (key_down(Key::Left) &&
      !player.collision_x(-1) &&
      player.x > x_min)
      player.move(-1, 0);
  if (key_down(Key::Right) &&
      !player.collision_x(1) &&
      player.x < x_max)
    player.move(+1, 0);

  if (key_pressed(Key::Jump) &&
      can_jump &&
      player.jumping == 0 &&
      !player.collision_y(-1)) {
    player.jumping = 1;
    player.move(0, -1);
    jump_clock = clock();
  }
  if (player.jumping != 0) {
    if (player.jumping < jump_height && get_dur(jump_clock) > jump_time1) {
      if (!player.collision_y(-1)) {
        player.move(0, -1);
        player.jumping++;
        jump_clock = clock();
      } else {
        player.jumping = jump_height;
      }
    } else if (player.jumping == jump_height && get_dur(jump_clock) > jump_time2) {
      player.jumping = 0;
    }
  }
  if (!player.jumping && !player.collision_y(1))
    player.move(0, +1);

  char b = get_block(player.x, player.y);
  if (b == '/' || b == '\\' || b == '<' || b == '>')
    player.move_to(player.x_spawn, player.y_spawn);
}

void print_text(int x, int y, string text, int delay) {
  for (int i = 0; i < text.size(); i++) {
    select_length(get_pos(x + i, y), 1);
    replace(text.substr(i, 1));
    Sleep(delay);
  }
}

void intro() {
  static int progress = 0;
  switch (progress) {
  case 0:
    print_text(4, 2, "Move with left/right.", text_speed);

    progress++;
    break;
  case 1:
    update_play(false);
    if (player.x == 17) {
      print_text(4, 4, "Jump with up.", text_speed);
      print_text(4, 6, "Stand on x.", text_speed);
      progress++;
    }
    break;
  case 2:
    update_play();
    if (player.x == 22) {
      print_text(4, 8, "Collect ? for ???.", text_speed);
      progress++;
    }
    break;
  case 3:
    update_play(true, 0, 33);
    if (get_block(player.x, player.y) == '?') {
      print_text(4, 10, "Avoid /\\.", text_speed);
      progress++;
    }
    break;
  case 4:
    update_play();
    if (player.x == 39) {
      print_text(4, 14, "Finish lvl by reaching O.", text_speed);
      progress++;
    }
    break;
  case 5:
    update_play();
    if (get_block(player.x, player.y) == 'O') {
      load_level(1);
    }
    break;
  }
}

void lvl1() {
  static int progress = 0;
  switch (progress) {
  case 0:
    print_text(4, 2, "Also avoid > and <.", text_speed);
    progress++;
    break;
  case 1:
    update_play();
    break;
  }
}

void update_game() {
  switch (lvl) {
  case 0:
    intro();
    break;
  case 1:
    lvl1();
    break;
  }
}

#ifdef CONSOLE
int main(int argc, char **argv) {
#else
int WinMain(HINSTANCE a0, HINSTANCE a1, LPSTR a2, int a3) {
#endif
  start_notepad();
  if (hwnd_notepad == NULL || hwnd_edit == NULL) {
    puts("error");
    return 1;
  }

  load_level(0);
  
  while (true) {
    //dt = ((double)clock() - game_clock) / CLOCKS_PER_SEC * 1000;
    if (get_dur(game_clock) < frame_time) continue;
    game_clock = clock();
    update_key_state();

    if (key_pressed(Key::Exit))
      break;
    if (key_pressed(Key::Redraw))
      redraw();
    
    update_game();

    update_key_state_old();
  }
  close_notepad();

  return 0;
}