1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2020, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 
21 #include "object/auto/autoflag.h"
22 
23 #include "graphics/engine/terrain.h"
24 
25 #include "math/geometry.h"
26 
27 #include "object/old_object.h"
28 
29 
30 // Object's constructor.
31 
CAutoFlag(COldObject * object)32 CAutoFlag::CAutoFlag(COldObject* object) : CAuto(object)
33 {
34     Init();
35 }
36 
37 // Object's destructor.
38 
~CAutoFlag()39 CAutoFlag::~CAutoFlag()
40 {
41 }
42 
43 
44 // Destroys the object.
45 
DeleteObject(bool bAll)46 void CAutoFlag::DeleteObject(bool bAll)
47 {
48     CAuto::DeleteObject(bAll);
49 }
50 
51 
52 // Initialize the object.
53 
Init()54 void CAutoFlag::Init()
55 {
56     Math::Vector    wind;
57     float       angle;
58 
59     m_time = 0.0f;
60     m_param = 0;
61     m_progress = 0.0f;
62 
63     wind = m_terrain->GetWind();
64     angle = Math::RotateAngle(wind.x, -wind.z);
65     m_object->SetRotationY(angle);  // directs the flag in the wind
66 
67     m_strong = wind.Length();
68 }
69 
70 
71 // Beginning of an action (1 = shakes).
72 
Start(int param)73 void CAutoFlag::Start(int param)
74 {
75     if ( m_param == 0 )
76     {
77         m_param = param;
78         m_progress = 0.0f;
79     }
80 }
81 
82 
83 // Management of an event.
84 
EventProcess(const Event & event)85 bool CAutoFlag::EventProcess(const Event &event)
86 {
87     float   angle;
88     int     i;
89 
90     CAuto::EventProcess(event);
91 
92     if ( m_engine->GetPause() )  return true;
93     if ( event.type != EVENT_FRAME )  return true;
94 
95     if ( m_param == 1 )  // shakes?
96     {
97         m_progress += event.rTime*(1.0f/2.0f);
98         if ( m_progress < 1.0f )
99         {
100             angle = sinf(m_progress*Math::PI*8.0f)*0.3f*(1.0f-m_progress);
101             m_object->SetRotationX(angle);
102             angle = sinf(m_progress*Math::PI*4.0f)*0.3f*(1.0f-m_progress);
103             m_object->SetRotationZ(angle);
104         }
105         else
106         {
107             m_object->SetRotationX(0.0f);
108             m_object->SetRotationZ(0.0f);
109             m_param = 0;
110             m_progress = 0.0f;
111         }
112     }
113 
114     if ( m_strong == 0.0f )  return true;  // no wind?
115 
116     for ( i=0 ; i<4 ; i++ )
117     {
118         angle = sinf(m_time*6.0f+i*2.0f)*((i+2.0f)*0.1f);
119         m_object->SetPartRotationY(1+i, angle);
120     }
121 
122     return true;
123 }
124 
125 
126 // Returns an error due the state of the automation
127 
GetError()128 Error CAutoFlag::GetError()
129 {
130     return ERR_OK;
131 }
132