summaryrefslogtreecommitdiffstats
path: root/debian/fireflies/fireflies-2.08/src/bait.cc
blob: 417b9b72b2a11f06b5b258543dae5e2b0c432bb2 (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
#include "bait.h"
#include "modes.h"
#include "scene.h"

Bait::Bait()
    : Arrow()
{
    age = rand_real(0., 10.);
    fuzz = rand_real(0.7, 1.4);
    glow = false;
    attractor = 0;
    bait_start_mode(this, BMODE_NORMAL);

    hsv[0] = 30.0*rand_int(0, 12); // initial hue
    turn_delay = rand_real(1., 4.);
    turn_when = age + rand_real(0.5, turn_delay);
    pos = Vec3f(
	    rand_real(-world[0], world[0]),
	    rand_real(-world[1], world[1]),
	    rand_real(-world[2], world[2]));
    velocity = bspeed*unit_vec(-pos);

    for (int i = 0; i < 3; i++) {
	if (rand_int(0, 1)==0)	accel[i] = -baccel;
	else			accel[i] = baccel;
    }

    set_color();

#ifdef DEBUG
    cerr << "bait: " << bspeed << " and " << baccel
	 << "\tflies: " << fspeed << " and " << faccel << endl;
#endif
}

void Bait::draw()
{
    if (!scene.draw_bait)
	return;

    glPushMatrix();
    Arrow::draw();
    glPopMatrix();
}

void Bait::elapse(double t)
{
    hsv[0] += hue_rate*t;
    age += t;

    if (age >= mode_when)
	bait_start_mode(this, mode_next);
    while (stop_timer.is_ready(age))
	bait_stop_mode(this, stop_timer.pop());

    calc_accel();
    velocity += accel*t;
    clamp_vec(velocity, bspeed);
    pos += velocity*t;

    point(velocity);
    set_color();
}

void Bait::calc_accel()
{
    if (attractor) {
	accel = baccel*unit_vec((*attractor) - pos);
	return;
    }

    // time to turn
    if (age >= turn_when) {
	for (int i = 0; i < 3; i++) {
	    if (rand_int(0, 1) == 0)
		accel[i] = -SIGN(accel[i])*baccel;
	}
	turn_when = age + rand_real(0.5, turn_delay);
    }

    for (int i = 0; i < 3; i++) {
	if (pos[i] < -world[i])		accel[i] = baccel;
	else if (pos[i] > world[i])	accel[i] = -baccel;
    }
}

void Bait::set_color()
{
    // keep everything as before

    // clamp to my range
    while (hsv[0] > 360.f)
	hsv[0] -= 360.f;
    while (hsv[0] < 0.f)
	hsv[0] += 360.f;

    color = hsv_to_rgb(hsv);
}