-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwheel.c
152 lines (142 loc) · 3.34 KB
/
wheel.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
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
#include <math.h>
#include "menu.h"
#include "vector.h"
#include "random.h"
#include "input.h"
#include "elements.h"
#include "dot.h"
#include "wheel.h"
#include "save.h"
Wheel Wheel_wheels[Wheel_MAX];
Wheel* const Wheel_END = &Wheel_wheels[Wheel_MAX];
Wheel* Wheel_next = Wheel_wheels;
extern const char Wheel_frames[16][32][32];
void Wheel_update(void) {
if (Menu_dragging) {
Wheel_FOR (w) {
Point d = {Pen_x-w->x, Pen_y-w->y};
if (Vec_fastDist(d)<16)
w->vel -= d.x*(Pen_y-Pen_oldy)-d.y*(Pen_x-Pen_oldx);
}
}
Wheel_FOR (w) {
const char (*frame)[32] = Wheel_frames[(int)w->angle];
for (axis y=0; y<32; y++) {
for (axis x=0; x<32; x++) {
if (frame[y][x]=='.') {
Block* cell = Block_at(w->x+x-16, w->y+y-16);
real wind = cell->vel.x*(y-15.5) - cell->vel.y*(x-15.5);
w->vel += 0.0001*wind;
}
Dot* p = Dot_at[w->y+y-16][w->x+x-16];
if (p>=Dot_0 && frame[y][x]==' ' && y!=31 && frame[y+1][x]=='.') {
real weight = 0*(y-15.5)-1*(x-15.5);
w->vel += weight*ELEMENTS[p->type].wheelWeight*0.0001;
}
}
}
w->vel *= 0.99;
w->vel = clamp(w->vel, -2, 2);
real g = (int)(w->angle+16);
w->angle += w->vel;
g = (int)(w->angle+16)-g;
if (w->angle<0)
w->angle += 16;
if (w->angle>=16)
w->angle -= 16;
if (g) {
typedef struct WheelDot {
Dot* part;
real x,y;
} WheelDot;
WheelDot parts[1024];
WheelDot* next = parts;
frame = Wheel_frames[(int)floor(w->angle)];
for (axis y=0; y<32; y++) {
for (axis x=0; x<32; x++) {
if (frame[y][x]==' ') {
Dot* p = Dot_at[w->y+y-16][w->x+x-16];
if (p>=Dot_0)
*next++ = (WheelDot){p, (y-15.5)*g*0.1, -(x-15.5)*g*0.1};
}
}
}
for (WheelDot* wp=parts; wp<next; wp++) {
Dot* part = wp->part;
*Dot_pos2(part->pos) = Dot_EMPTY;
Dot* p = *Dot_pos(part->pos.x+wp->x, part->pos.y);
if (p<Dot_BLOCK)
part->pos.x += wp->x;
p = *Dot_pos(part->pos.x, part->pos.y+wp->y);
if (p<Dot_BLOCK)
part->pos.y += wp->y;
*Dot_pos2(part->pos) = part;
switch (part->type) {
when(Elem_WOOD):;
if (Rnd_perchance(20))
part->type = Elem_POWDER;
when(Elem_ICE):;
part->type = Elem_SNOW;
when(Elem_FUSE):;
part->type = Elem_GUNPOWDER;
when(Elem_PUMP):;
if (Rnd_perchance(10)) {
part->type = Elem_SPARK;
part->charge = 0;
}
}
}
}
}
}
void Wheel_remove(Wheel* w) {
if (w != Wheel_next) {
Wheel_next--;
*w = *Wheel_next;
}
}
void Wheel_create(axis x, axis y) {
Wheel_FOR (w) {
if (x==w->x && y==w->y)
return;
}
if (Wheel_next >= Wheel_END) return;
*Wheel_next = (Wheel){
.x = x,
.y = y,
.angle = 0.5,
.vel = 0,
};
Wheel_next++;
}
void Wheel_update1(void) {
// remove old wheel parts from grid
Wheel_FOR (w) {
for (axis y=0; y<32; y++)
for (axis x=0; x<32; x++) {
Dot** p = Dot_pos(w->x-16+x, w->y-16+y);
if (*p == Dot_WHEEL)
*p = Dot_EMPTY;
}
}
// add new ones
Wheel_FOR (w) {
const char (*frame)[32] = Wheel_frames[(int)w->angle];
for (axis y=0; y<32; y++)
for (axis x=0; x<32; x++) {
if (frame[y][x]=='.') {
Dot** p = Dot_pos(w->x-16+x, w->y-16+y);
if (*p <= Dot_BGFAN)
*p = Dot_WHEEL;
}
}
}
}
void Wheel_save(SavePixel save[H][W]) {
Wheel_FOR (w) {
save[w->y-8][w->x-8] = (SavePixel){Elem_WHEEL, 0};
}
}
void Wheel_reset(void) {
Wheel_next = Wheel_wheels;
}