blob: e3a65f0282f31f9d38489b1caf35a3832e5fb521 (
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
|
// BehaviorBackUp.h
// Back up for a short duration
// Author: Michael Booth, March 2007
//========= Copyright Valve Corporation, All rights reserved. ============//
#ifndef _BEHAVIOR_BACK_UP_H_
#define _BEHAVIOR_BACK_UP_H_
//----------------------------------------------------------------------------------------------
/**
* Move backwards for a short duration away from a given position.
* Useful to dislodge ourselves if we get stuck while following our path.
*/
template < typename Actor >
class BehaviorBackUp : public Action< Actor >
{
public:
BehaviorBackUp( const Vector &avoidPos );
virtual ActionResult< Actor > OnStart( Actor *me, Action< Actor > *priorAction );
virtual ActionResult< Actor > Update( Actor *me, float interval );
virtual EventDesiredResult< Actor > OnStuck( Actor *me );
virtual const char *GetName( void ) const { return "BehaviorBackUp"; }
private:
CountdownTimer m_giveUpTimer;
CountdownTimer m_backupTimer;
CountdownTimer m_jumpTimer;
Vector m_way;
Vector m_avoidPos;
};
//----------------------------------------------------------------------------------------------
template < typename Actor >
inline BehaviorBackUp< Actor >::BehaviorBackUp( const Vector &avoidPos )
{
m_avoidPos = avoidPos;
}
//----------------------------------------------------------------------------------------------
template < typename Actor >
inline ActionResult< Actor > BehaviorBackUp< Actor >::OnStart( Actor *me, Action< Actor > *priorAction )
{
ILocomotion *mover = me->GetLocomotionInterface();
// don't back off if we're on a ladder
if ( mover && mover->IsUsingLadder() )
{
return Done();
}
float backupTime = RandomFloat( 0.3f, 0.5f );
m_backupTimer.Start( backupTime );
m_jumpTimer.Start( 1.5f * backupTime );
m_giveUpTimer.Start( 2.5f * backupTime );
m_way = me->GetPosition() - m_avoidPos;
m_way.NormalizeInPlace();
return Continue();
}
//----------------------------------------------------------------------------------------------
template < typename Actor >
inline ActionResult< Actor > BehaviorBackUp< Actor >::Update( Actor *me, float interval )
{
if ( m_giveUpTimer.IsElapsed() )
{
return Done();
}
// if ( m_jumpTimer.HasStarted() && m_jumpTimer.IsElapsed() )
// {
// me->GetLocomotionInterface()->Jump();
// m_jumpTimer.Invalidate();
// }
ILocomotion *mover = me->GetLocomotionInterface();
if ( mover )
{
Vector goal;
if ( m_backupTimer.IsElapsed() )
{
// move towards bad spot
goal = m_avoidPos; // me->GetPosition() - 100.0f * m_way;
}
else
{
// move away from bad spot
goal = me->GetPosition() + 100.0f * m_way;
}
mover->Approach( goal );
}
return Continue();
}
//----------------------------------------------------------------------------------------------
template < typename Actor >
inline EventDesiredResult< Actor > BehaviorBackUp< Actor >::OnStuck( Actor *me )
{
return TryToSustain( RESULT_IMPORTANT, "Stuck while trying to back up" );
}
#endif // _BEHAVIOR_BACK_UP_H_
|