1 // -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi:tw=80:et:ts=2:sts=2
3 //
4 // -----------------------------------------------------------------------
5 //
6 // This file is part of RLVM, a RealLive virtual machine clone.
7 //
8 // -----------------------------------------------------------------------
9 //
10 // Copyright (C) 2013 Elliot Glaysher
11 //
12 // This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 // -----------------------------------------------------------------------
26 
27 #include "systems/base/object_mutator.h"
28 
29 #include "machine/rlmachine.h"
30 #include "systems/base/event_system.h"
31 #include "systems/base/graphics_object.h"
32 #include "systems/base/graphics_object_data.h"
33 #include "systems/base/graphics_system.h"
34 #include "systems/base/parent_graphics_object_data.h"
35 #include "systems/base/system.h"
36 #include "utilities/math_util.h"
37 
ObjectMutator(int repr,const std::string & name,int creation_time,int duration_time,int delay,int type)38 ObjectMutator::ObjectMutator(int repr,
39                              const std::string& name,
40                              int creation_time,
41                              int duration_time,
42                              int delay,
43                              int type)
44     : repr_(repr),
45       name_(name),
46       creation_time_(creation_time),
47       duration_time_(duration_time),
48       delay_(delay),
49       type_(type) {
50 }
51 
52 ObjectMutator::ObjectMutator(const ObjectMutator& mutator) = default;
53 
~ObjectMutator()54 ObjectMutator::~ObjectMutator() {}
55 
operator ()(RLMachine & machine,GraphicsObject & object)56 bool ObjectMutator::operator()(RLMachine& machine, GraphicsObject& object) {
57   unsigned int ticks = machine.system().event().GetTicks();
58   if (ticks > (creation_time_ + delay_)) {
59     PerformSetting(machine, object);
60     machine.system().graphics().mark_object_state_as_dirty();
61   }
62   return ticks > (creation_time_ + delay_ + duration_time_);
63 }
64 
OperationMatches(int repr,const std::string & name) const65 bool ObjectMutator::OperationMatches(int repr, const std::string& name) const {
66   return repr_ == repr && name_ == name;
67 }
68 
GetValueForTime(RLMachine & machine,int start,int end)69 int ObjectMutator::GetValueForTime(RLMachine& machine, int start, int end) {
70   unsigned int ticks = machine.system().event().GetTicks();
71   if (ticks < (creation_time_ + delay_)) {
72     return start;
73   } else if (ticks < (creation_time_ + delay_ + duration_time_)) {
74     return InterpolateBetween(creation_time_ + delay_,
75                               ticks,
76                               creation_time_ + delay_ + duration_time_,
77                               start,
78                               end,
79                               type_);
80   } else {
81     return end;
82   }
83 }
84 
85 // -----------------------------------------------------------------------
86 
OneIntObjectMutator(const std::string & name,int creation_time,int duration_time,int delay,int type,int start_value,int target_value,Setter setter)87 OneIntObjectMutator::OneIntObjectMutator(const std::string& name,
88                                          int creation_time,
89                                          int duration_time,
90                                          int delay,
91                                          int type,
92                                          int start_value,
93                                          int target_value,
94                                          Setter setter)
95     : ObjectMutator(-1, name, creation_time, duration_time, delay, type),
96       startval_(start_value),
97       endval_(target_value),
98       setter_(setter) {}
99 
~OneIntObjectMutator()100 OneIntObjectMutator::~OneIntObjectMutator() {}
101 
102 OneIntObjectMutator::OneIntObjectMutator(const OneIntObjectMutator& rhs) =
103   default;
104 
SetToEnd(RLMachine & machine,GraphicsObject & object)105 void OneIntObjectMutator::SetToEnd(RLMachine& machine, GraphicsObject& object) {
106   (object.*setter_)(endval_);
107 }
108 
Clone() const109 ObjectMutator* OneIntObjectMutator::Clone() const {
110   return new OneIntObjectMutator(*this);
111 }
112 
PerformSetting(RLMachine & machine,GraphicsObject & object)113 void OneIntObjectMutator::PerformSetting(RLMachine& machine,
114                                          GraphicsObject& object) {
115   int value = GetValueForTime(machine, startval_, endval_);
116   (object.*setter_)(value);
117 }
118 
119 // -----------------------------------------------------------------------
120 
RepnoIntObjectMutator(const std::string & name,int creation_time,int duration_time,int delay,int type,int repno,int start_value,int target_value,Setter setter)121 RepnoIntObjectMutator::RepnoIntObjectMutator(const std::string& name,
122                                              int creation_time,
123                                              int duration_time,
124                                              int delay,
125                                              int type,
126                                              int repno,
127                                              int start_value,
128                                              int target_value,
129                                              Setter setter)
130     : ObjectMutator(repno, name, creation_time, duration_time, delay, type),
131       repno_(repno),
132       startval_(start_value),
133       endval_(target_value),
134       setter_(setter) {}
135 
~RepnoIntObjectMutator()136 RepnoIntObjectMutator::~RepnoIntObjectMutator() {}
137 
138 RepnoIntObjectMutator::RepnoIntObjectMutator(const RepnoIntObjectMutator& rhs) =
139   default;
140 
SetToEnd(RLMachine & machine,GraphicsObject & object)141 void RepnoIntObjectMutator::SetToEnd(RLMachine& machine,
142                                      GraphicsObject& object) {
143   (object.*setter_)(repno_, endval_);
144 }
145 
Clone() const146 ObjectMutator* RepnoIntObjectMutator::Clone() const {
147   return new RepnoIntObjectMutator(*this);
148 }
149 
PerformSetting(RLMachine & machine,GraphicsObject & object)150 void RepnoIntObjectMutator::PerformSetting(RLMachine& machine,
151                                            GraphicsObject& object) {
152   int value = GetValueForTime(machine, startval_, endval_);
153   (object.*setter_)(repno_, value);
154 }
155 
156 // -----------------------------------------------------------------------
157 
TwoIntObjectMutator(const std::string & name,int creation_time,int duration_time,int delay,int type,int start_one,int target_one,Setter setter_one,int start_two,int target_two,Setter setter_two)158 TwoIntObjectMutator::TwoIntObjectMutator(const std::string& name,
159                                          int creation_time,
160                                          int duration_time,
161                                          int delay,
162                                          int type,
163                                          int start_one,
164                                          int target_one,
165                                          Setter setter_one,
166                                          int start_two,
167                                          int target_two,
168                                          Setter setter_two)
169     : ObjectMutator(-1, name, creation_time, duration_time, delay, type),
170       startval_one_(start_one),
171       endval_one_(target_one),
172       setter_one_(setter_one),
173       startval_two_(start_two),
174       endval_two_(target_two),
175       setter_two_(setter_two) {}
176 
~TwoIntObjectMutator()177 TwoIntObjectMutator::~TwoIntObjectMutator() {}
178 
179 TwoIntObjectMutator::TwoIntObjectMutator(const TwoIntObjectMutator& rhs) =
180   default;
181 
SetToEnd(RLMachine & machine,GraphicsObject & object)182 void TwoIntObjectMutator::SetToEnd(RLMachine& machine, GraphicsObject& object) {
183   (object.*setter_one_)(endval_one_);
184   (object.*setter_two_)(endval_two_);
185 }
186 
Clone() const187 ObjectMutator* TwoIntObjectMutator::Clone() const {
188   return new TwoIntObjectMutator(*this);
189 }
190 
PerformSetting(RLMachine & machine,GraphicsObject & object)191 void TwoIntObjectMutator::PerformSetting(RLMachine& machine,
192                                          GraphicsObject& object) {
193   int value = GetValueForTime(machine, startval_one_, endval_one_);
194   (object.*setter_one_)(value);
195 
196   value = GetValueForTime(machine, startval_two_, endval_two_);
197   (object.*setter_two_)(value);
198 }
199