njin
Engine 2D gọn nhẹ để tự tay làm game theo ý mình. Vibe coding cũng ổn luôn
Loading...
Searching...
No Matches
_comps.h
1#pragma once
2
3#include "_math.h"
4#include "_types.h"
5#include <entt/entity/entity.hpp>
6namespace njin {
9
14struct transform {
16 f32 rot = 0.0f;
17 f32 scale = 1.0f;
18};
19
25struct camera_2d {
30 f32 zoom = 1.0f;
31};
32
37struct camera_on {};
38
44struct sprite {
51 vec2 origin{0.5f, 0.5f};
52 rgba tint{1.0f, 1.0f, 1.0f, 1.0f};
53 i32 layer = 0;
57 bool flip_x = false;
58 bool flip_y = false;
59 bool visible = true;
76};
77
85 i32 first = 0;
86 i32 count = 1;
87 f32 fps = 10.0f;
88 bool loop = true;
89 bool playing = true;
90 f32 time = 0.0f;
91 i32 frame = 0;
92 bool finished = false;
93};
94
104inline void anim_play(sprite_anim &anim, i32 first, i32 count, f32 fps,
105 bool loop = true) {
106 if (anim.first == first && anim.count == count && anim.playing &&
107 !anim.finished)
108 return;
109 anim.first = first;
110 anim.count = count;
111 anim.fps = fps;
112 anim.loop = loop;
113 anim.playing = true;
114 anim.time = 0.0f;
115 anim.frame = 0;
116 anim.finished = false;
117}
118
125
139struct child_of {
140 entt::entity parent = entt::null;
143};
144
153 const transform &local) {
154 return transform{.pos = parent.pos + rotate(local.pos * parent.scale, parent.rot),
155 .rot = parent.rot + local.rot,
156 .scale = parent.scale * local.scale};
157}
158
168 const transform &world) {
169 const f32 scale = parent.scale != 0.0f ? parent.scale : 1.0f;
170 return transform{.pos = rotate(world.pos - parent.pos, -parent.rot) / scale,
171 .rot = world.rot - parent.rot,
172 .scale = world.scale / scale};
173}
174
175} // namespace njin
transform transform_relative(const transform &parent, const transform &world)
Phép ngược của transform_combine(): transform tương đối của một entity so với cha,...
Definition _comps.h:167
void anim_play(sprite_anim &anim, i32 first, i32 count, f32 fps, bool loop=true)
Đổi sang một animation khác trong cùng sprite sheet.
Definition _comps.h:104
transform transform_combine(const transform &parent, const transform &local)
Kết hợp transform của cha với transform tương đối của con.
Definition _comps.h:152
f32 fps(const context &ctx)
Trả về FPS mục tiêu đã cấu hình (target_fps).
vec2 rotate(vec2 v, f32 degrees)
Xoay vector theo chiều kim đồng hồ trên màn hình (trục y hướng xuống).
Definition _math.h:83
entt::registry & world(context &ctx)
Trả về registry của EnTT chứa mọi entity và component của game.
std::int32_t i32
Số nguyên có dấu 32 bit.
Definition _types.h:14
float f32
Số thực 32 bit.
Definition _types.h:28
2D camera.
Definition _comps.h:25
f32 zoom
1 là không phóng, 2 là mọi thứ to gấp đôi. Giá trị <= 0 được coi là 1.
Definition _comps.h:30
vec2 offset
Vị trí trên màn hình (pixel) nơi transform.pos được vẽ.
Definition _comps.h:28
Tag đánh dấu camera đang được dùng để vẽ và cho w2scr()/scr2w().
Definition _comps.h:37
Gắn entity vào một entity cha: vũ khí trong tay nhân vật, bánh xe của xe.
Definition _comps.h:139
entt::entity parent
Entity cha.
Definition _comps.h:140
transform local
Vị trí, góc xoay, tỉ lệ so với cha.
Definition _comps.h:141
bool destroy_with_parent
Hủy cùng cha.
Definition _comps.h:142
Hình chữ nhật thẳng trục: pos là góc trên trái, size là rộng và cao.
Definition _math.h:13
Màu gồm đỏ, xanh lá, xanh dương và alpha.
Definition _types.h:64
Định danh của một scene, tạo bởi scene_register().
Definition _types.h:139
Gắn entity vào một scene.
Definition _comps.h:122
scene_handle scene
Scene sở hữu entity.
Definition _comps.h:123
Animation theo frame từ một sprite sheet.
Definition _comps.h:83
bool finished
Đã chạy hết (chỉ khi không lặp).
Definition _comps.h:92
vec2 frame_size
Kích thước một frame, tính bằng pixel.
Definition _comps.h:84
bool loop
Lặp lại khi hết.
Definition _comps.h:88
i32 count
Số frame của animation.
Definition _comps.h:86
i32 frame
Frame hiện tại, từ 0 đến count - 1.
Definition _comps.h:91
f32 fps
Số frame mỗi giây.
Definition _comps.h:87
i32 first
Số thứ tự frame đầu tiên của animation.
Definition _comps.h:85
bool playing
Đang chạy. Đặt false để dừng ở frame hiện tại.
Definition _comps.h:89
f32 time
Thời gian đã chạy, do engine cập nhật.
Definition _comps.h:90
Ảnh vẽ tại vị trí của entity.
Definition _comps.h:44
texture_handle emissive
Bản đồ phát sáng: chỗ nào có màu là chỗ đó tự sáng, không cần đèn (mắt quái, cửa sổ có đèn,...
Definition _comps.h:74
rect source
Vùng trong ảnh, tính bằng pixel.
Definition _comps.h:48
bool flip_y
Lật dọc.
Definition _comps.h:58
texture_handle material
Bản đồ vật liệu của sprite cho ánh sáng PBR (njin_light.h), xếp kênh theo kiểu "MRA" của raylib: đỏ l...
Definition _comps.h:70
bool visible
Ẩn mà không cần gỡ component.
Definition _comps.h:59
i32 layer
Lớp vẽ: lớp nhỏ vẽ trước, lớp lớn đè lên.
Definition _comps.h:53
texture_handle normal
Normal map của sprite, để đèn (njin_light.h) làm sprite nổi khối.
Definition _comps.h:64
f32 sort_offset
Cộng vào transform.pos.y khi sắp theo Y (xem draw_set_y_sort()): đặt bằng khoảng từ điểm neo xuống ch...
Definition _comps.h:56
rgba tint
Màu nhân vào ảnh.
Definition _comps.h:52
bool flip_x
Lật ngang.
Definition _comps.h:57
vec2 origin
Điểm neo, tính theo tỉ lệ kích thước: {0, 0} là góc trên trái, {0.5, 0.5} là tâm.
Definition _comps.h:51
texture_handle texture
Ảnh cần vẽ.
Definition _comps.h:45
f32 emissive_power
Độ sáng của emissive, từ 0 đến 8. Lớn hơn 1 là sáng hơn màu ảnh, tràn ra bloom.
Definition _comps.h:75
Định danh của một texture, tạo bởi texture_load().
Definition _types.h:146
Vị trí, góc xoay và tỉ lệ của một entity trong thế giới.
Definition _comps.h:14
vec2 pos
Vị trí trong thế giới.
Definition _comps.h:15
f32 rot
Góc xoay tính bằng độ, theo chiều kim đồng hồ.
Definition _comps.h:16
f32 scale
Tỉ lệ. 1 là kích thước gốc.
Definition _comps.h:17
Vector 2 chiều. Dùng cho vị trí, kích thước, độ dời.
Definition _types.h:39