-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy.c
More file actions
139 lines (108 loc) · 3.7 KB
/
enemy.c
File metadata and controls
139 lines (108 loc) · 3.7 KB
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
// SPDX-License-Identifier: MIT
// Copyright (C) 2026 p1k0chu
#include "enemy.h"
#include "git2/commit.h"
#include "git2/revwalk.h"
#include "main.h"
#include "my_math.h"
#include "utils.h"
#include <SDL3/SDL_stdinc.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int spawn_enemy(Enemy *const dst,
const Vec2d spawn,
const Vec2d spawn_src,
const double speed,
const SDL_Color color,
double rotation,
const Vec2d move_direction,
const unsigned int pattern_id) {
git_oid oid;
if (git_revwalk_next(&oid, walker) != 0)
return 0;
git_commit *commit;
int error = git_commit_lookup(&commit, repo, &oid);
if (error < 0)
libgit_panic(error);
const char *line = git_commit_summary(commit);
if (line == NULL)
libgit_panic(0);
dst->speed = speed;
dst->pattern_id = pattern_id;
SDL_Surface *surface = TTF_RenderText_Blended(font, line, 0, color);
if (!surface)
sdl_die("");
git_commit_free(commit);
dst->texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_DestroySurface(surface);
if (!dst->texture)
sdl_die("");
SDL_GetTextureSize(dst->texture, &dst->rect.w, &dst->rect.h);
dst->rect.x = spawn.x - dst->rect.w * spawn_src.x;
dst->rect.y = spawn.y - dst->rect.h * spawn_src.y;
double radian = NAN;
if (SDL_isnan(rotation)) {
radian = SDL_atan2(player.y - (dst->rect.y + dst->rect.h / 2.0),
player.x - (dst->rect.x + dst->rect.w / 2.0));
rotation = radian * 180.0 / SDL_PI_D;
}
while (rotation < 0.0) rotation += 360.0;
if (rotation > 90.0 && rotation < 270.0) {
rotation -= 180.0;
}
dst->rotation = rotation;
if (move_direction.x == 0 && move_direction.y == 0) {
if (SDL_isnan(radian)) {
radian = SDL_atan2(player.y - (dst->rect.y + dst->rect.h / 2.0),
player.x - (dst->rect.x + dst->rect.w / 2.0));
}
dst->move_direction.x = SDL_cos(radian);
dst->move_direction.y = SDL_sin(radian);
} else {
dst->move_direction = move_direction;
}
return 1;
}
int collide(Player *player, Enemy *enemy) {
Vec2d normals[4] = {// player's normals
{1, 0},
{0, 1}};
Enemy_get_normals(enemy, normals + 2);
Vec2d player_points[4];
Player_get_points(player, player_points);
Vec2d enemy_points[4];
Enemy_get_points(enemy, enemy_points);
return polygons_collide(normals, 4, player_points, 4, enemy_points, 4);
}
void Enemy_get_points(const Enemy *this, Vec2d dst[4]) {
const Vec2d e_center = {this->rect.x + this->rect.w / 2, this->rect.y + this->rect.h / 2};
const double rad = this->rotation * SDL_PI_D / 180.0;
// top left
Vec2d tmp = {-this->rect.w / 2, -this->rect.h / 2};
tmp = Vec2d_rotate(&tmp, rad);
dst[0] = Vec2d_add(&tmp, &e_center);
// top right
tmp.x = this->rect.w / 2;
tmp.y = -this->rect.h / 2;
tmp = Vec2d_rotate(&tmp, rad);
dst[1] = Vec2d_add(&tmp, &e_center);
// bottom left
tmp.x = -this->rect.w / 2;
tmp.y = this->rect.h / 2;
tmp = Vec2d_rotate(&tmp, rad);
dst[2] = Vec2d_add(&tmp, &e_center);
// bottom right
tmp.x = this->rect.w / 2;
tmp.y = this->rect.h / 2;
tmp = Vec2d_rotate(&tmp, rad);
dst[3] = Vec2d_add(&tmp, &e_center);
}
void Enemy_get_normals(const Enemy *this, Vec2d dst[2]) {
double rad = this->rotation * SDL_PI_D / 180.0;
dst[0].x = SDL_cos(rad);
dst[0].y = SDL_sin(rad);
rad += SDL_PI_D / 2;
dst[1].x = SDL_cos(rad);
dst[1].y = SDL_sin(rad);
}