aboutsummaryrefslogtreecommitdiff
path: root/src/vehicles/Door.cpp
blob: 1b3f9e8f6d1427b4a73f5a18b67f5508304adaaa (plain) (blame)
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
#include "common.h"

#include "Vehicle.h"
#include "Door.h"

CDoor::CDoor(void)
{
	memset(this, 0, sizeof(*this));
}

void
CDoor::Open(float ratio)
{
	float open;

	m_fPrevAngle = m_fAngle;
	open = RetAngleWhenOpen();
	if(ratio < 1.0f){
		m_fAngle = open*ratio;
		if(m_fAngle == 0.0f)
			m_fAngVel = 0.0f;
	}else{
		m_nDoorState = DOORST_OPEN;
		m_fAngle = open;
	}
}

void
CDoor::Process(CVehicle *vehicle)
{
	static CVector vecOffset(1.0f, 0.0f, 0.0f);
	CVector speed = vehicle->GetSpeed(vecOffset);
	CVector vecSpeedDiff = speed - m_vecSpeed;
	vecSpeedDiff = Multiply3x3(vecSpeedDiff, vehicle->GetMatrix());

	// air resistance
	float fSpeedDiff = 0.0f;	// uninitialized in game
	switch(m_nAxis){
	case 0:	// x-axis
		if(m_nDirn)
			fSpeedDiff = vecSpeedDiff.y + vecSpeedDiff.z;
		else
			fSpeedDiff = -(vecSpeedDiff.y + vecSpeedDiff.z);
		break;

	// we don't support y axis apparently?

	case 2:	// z-axis
		if(m_nDirn)
			fSpeedDiff = -(vecSpeedDiff.y + vecSpeedDiff.x);
		else
			fSpeedDiff = vecSpeedDiff.y - vecSpeedDiff.x;
		break;
	}
	fSpeedDiff = Clamp(fSpeedDiff, -0.2f, 0.2f);
	if(Abs(fSpeedDiff) > 0.002f)
		m_fAngVel += fSpeedDiff;
	m_fAngVel *= 0.945f;
	m_fAngVel = Clamp(m_fAngVel, -0.3f, 0.3f);

	m_fAngle += m_fAngVel;
	m_nDoorState = DOORST_SWINGING;
	if(m_fAngle > m_fMaxAngle){
		m_fAngle = m_fMaxAngle;
		m_fAngVel *= -0.8f;
		m_nDoorState = DOORST_OPEN;
	}
	if(m_fAngle < m_fMinAngle){
		m_fAngle = m_fMinAngle;
		m_fAngVel *= -0.8f;
		m_nDoorState = DOORST_CLOSED;
	}
	m_vecSpeed = speed;
}

float
CDoor::RetAngleWhenClosed(void)
{
	if(Abs(m_fMaxAngle) < Abs(m_fMinAngle))
		return m_fMaxAngle;
	else
		return m_fMinAngle;
}

float
CDoor::RetAngleWhenOpen(void)
{
	if(Abs(m_fMaxAngle) < Abs(m_fMinAngle))
		return m_fMinAngle;
	else
		return m_fMaxAngle;
}

float
CDoor::GetAngleOpenRatio(void)
{
	float open = RetAngleWhenOpen();
	if(open == 0.0f)
		return 0.0f;
	return m_fAngle/open;
}

bool
CDoor::IsFullyOpen(void)
{
	// why -0.5? that's around 28 deg less than fully open
	if(Abs(m_fAngle) < Abs(RetAngleWhenOpen()) - 0.5f)
		return false;
	return true;
}

bool
CDoor::IsClosed(void)
{
	return m_fAngle == RetAngleWhenClosed();
}


CTrainDoor::CTrainDoor(void)
{
	memset(this, 0, sizeof(*this));
}

void
CTrainDoor::Open(float ratio)
{
	float open;

	m_fPrevPosn = m_fPosn;
	open = RetTranslationWhenOpen();
	if(ratio < 1.0f){
		m_fPosn = open*ratio;
	}else{
		m_nDoorState = DOORST_OPEN;
		m_fPosn = open;
	}
}

float
CTrainDoor::RetTranslationWhenClosed(void)
{
	if(Abs(m_fClosedPosn) < Abs(m_fOpenPosn))
		return m_fClosedPosn;
	else
		return m_fOpenPosn;
}

float
CTrainDoor::RetTranslationWhenOpen(void)
{
	if(Abs(m_fClosedPosn) < Abs(m_fOpenPosn))
		return m_fOpenPosn;
	else
		return m_fClosedPosn;
}

bool
CTrainDoor::IsFullyOpen(void)
{
	// 0.5f again...
	if(Abs(m_fPosn) < Abs(RetTranslationWhenOpen()) - 0.5f)
		return false;
	return true;
}

bool
CTrainDoor::IsClosed(void)
{
	return m_fPosn == RetTranslationWhenClosed();
}