summaryrefslogtreecommitdiffhomepage
path: root/Stars45/NavLight.cpp
blob: 1be4fd55b625c2063bcd720a1a502e95d0eb1e00 (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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Navigation Light System class
*/

#include "NavLight.h"

#include "Clock.h"
#include "Bitmap.h"
#include "DataLoader.h"
#include "ContentBundle.h"

// +----------------------------------------------------------------------+

static Bitmap* images[4];

// +----------------------------------------------------------------------+

NavLight::NavLight(double p, double s)
: System(COMPUTER, 32, "Navigation Lights", 1, 0),
period(p), nlights(0), scale(s), enable(true)
{
    name = ContentBundle::GetInstance()->GetText("sys.nav-light");
    abrv = ContentBundle::GetInstance()->GetText("sys.nav-light.abrv");

    ZeroMemory(beacon,      sizeof(beacon));
    ZeroMemory(beacon_type, sizeof(beacon_type));
    ZeroMemory(pattern,     sizeof(pattern));
}

// +----------------------------------------------------------------------+

NavLight::NavLight(const NavLight& c)
: System(c), period(c.period), scale(c.scale),
nlights(0), enable(true)
{
    Mount(c);
    SetAbbreviation(c.Abbreviation());
    ZeroMemory(beacon,      sizeof(beacon));
    ZeroMemory(beacon_type, sizeof(beacon_type));

    nlights = c.nlights;

    for (int i = 0; i < nlights; i++) {
        loc[i]         = c.loc[i];
        pattern[i]     = c.pattern[i];
        beacon_type[i] = c.beacon_type[i];

        DriveSprite* rep = new DriveSprite(images[beacon_type[i]]);
        rep->Scale(c.scale);

        beacon[i] = rep;
    }

    offset = rand();
}

// +--------------------------------------------------------------------+

NavLight::~NavLight()
{
    for (int i = 0; i < nlights; i++)
    GRAPHIC_DESTROY(beacon[i]);
}

// +--------------------------------------------------------------------+

void
NavLight::Initialize()
{
    static int initialized = 0;
    if (initialized) return;

    DataLoader* loader = DataLoader::GetLoader();
    loader->LoadTexture("beacon1.pcx", images[0], Bitmap::BMP_TRANSLUCENT);
    loader->LoadTexture("beacon2.pcx", images[1], Bitmap::BMP_TRANSLUCENT);
    loader->LoadTexture("beacon3.pcx", images[2], Bitmap::BMP_TRANSLUCENT);
    loader->LoadTexture("beacon4.pcx", images[3], Bitmap::BMP_TRANSLUCENT);

    initialized = 1;
}

void
NavLight::Close()
{
}

// +--------------------------------------------------------------------+

void
NavLight::ExecFrame(double seconds)
{
    if (enable && power_on) {
        double t = (Clock::GetInstance()->GameTime()+offset) / 1000.0;
        DWORD  n = (int) (fmod(t, period) * 32 / period);
        DWORD  code = 1 << n;

        for (int i = 0; i < nlights; i++) {
            if (beacon[i]) {
                if (pattern[i] & code)
                beacon[i]->SetShade(1);
                else
                beacon[i]->SetShade(0);
            }
        }
    }
    else {
        for (int i = 0; i < nlights; i++) {
            if (beacon[i]) {
                beacon[i]->SetShade(0);
            }
        }
    }
}

void
NavLight::Enable()
{
    enable = true;
}

void
NavLight::Disable()
{
    enable = false;
}

void
NavLight::AddBeacon(Point l, DWORD ptn, int t)
{
    if (nlights < MAX_LIGHTS) {
        loc[nlights]         = l;
        pattern[nlights]     = ptn;
        beacon_type[nlights] = t;

        DriveSprite* rep = new DriveSprite(images[t]);
        rep->Scale(scale);

        beacon[nlights] = rep;
        nlights++;
    }
}

void
NavLight::SetPeriod(double p)
{
    period = p;
}

void
NavLight::SetPattern(int index, DWORD ptn)
{
    if (index >= 0 && index < nlights)
    pattern[index] = ptn;
}

void
NavLight::SetOffset(DWORD o)
{
    offset = o;
}

// +--------------------------------------------------------------------+

void
NavLight::Orient(const Physical* rep)
{
    System::Orient(rep);

    const Matrix& orientation = rep->Cam().Orientation();
    Point         ship_loc    = rep->Location();

    for (int i = 0; i < nlights; i++) {
        Point projector = (loc[i] * orientation) + ship_loc;
        if (beacon[i]) beacon[i]->MoveTo(projector);
    }
}