-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDino.pde
131 lines (111 loc) · 2.61 KB
/
Dino.pde
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
class Dino extends GameObject implements Comparable<Dino> {
Brain brain;
Genome genome;
float jump_percent;
boolean alive = true;
int score;
Dino() {
x = (int) random(100, 300);
y = 450;
w = 80;
h = 86;
jump_percent = 0;
genome = new Genome();
init_brain();
sprite = "walking_dino_1";
sprite_offset[0] = -4;
sprite_offset[1] = -2;
}
void init_brain() {
brain = new Brain(genome);
}
void update(Enemy nextEnemy, int speed) {
if (nextEnemy != null) {
float distance = nextEnemy.x - x; // d, x, y, w, h
float[] inputs = {distance / 900, (nextEnemy.x - 450) / (1400 - 450), (nextEnemy.y - 370) / (480 - 370), (nextEnemy.w - 30) / (146 - 30), (nextEnemy.h - 40) / (96 - 40), (y - 278) / (484 - 278), (speed - 15) / (15)};
brain.forward_feed(inputs);
process_output();
}
if (jumping()) {
update_jump();
}
}
void process_output() {
// 0 - jump, 1 - crouch, 2 - nothing
if (brain.outputs[0] >= 0.5) {
if (!crouching() && !jumping())
jump();
}
if (brain.outputs[1] < 0.5) {
if (crouching())
stop_crouch();
} else {
if (jumping())
stop_jump();
crouch();
}
}
void display() {
if (alive)
image(sprites.get(sprite), x + sprite_offset[0], y + sprite_offset[1]);
}
void update_jump() {
y = (int)(450 - ((-4 * jump_percent * (jump_percent - 1)) * 172));
jump_percent += 0.03;
if (jump_percent > 1) {
stop_jump();
}
}
void jump() {
jump_percent = 0.0001;
sprite = "standing_dino";
}
void stop_jump() {
jump_percent = 0;
y = 450;
sprite = "walking_dino_1";
}
void crouch() {
if (!crouching()) {
y = 484;
w = 110;
h = 52;
sprite = "crouching_dino_1";
}
}
void stop_crouch() {
y = 450;
w = 80;
h = 86;
sprite = "walking_dino_1";
}
boolean jumping() {
return jump_percent > 0;
}
boolean crouching() {
return w == 110;
}
void kill(int s) {
score = s;
alive = false;
}
void reset() {
alive = true;
score = 0;
}
void toggle_sprite() {
if (sprite.equals("walking_dino_1")) {
sprite = "walking_dino_2";
} else if (sprite.equals("walking_dino_2")) {
sprite = "walking_dino_1";
} else if (sprite.equals("crouching_dino_1")) {
sprite = "crouching_dino_2";
} else if (sprite.equals("crouching_dino_2")) {
sprite = "crouching_dino_1";
}
}
@Override
public int compareTo(Dino oD) {
return Integer.compare(this.score, oD.score);
}
}