Colobot
powered_object.h
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, 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 #pragma once
21 
22 #include "object/object.h"
24 
25 #include "object/interface/power_container_object.h"
26 
27 class CObject;
28 
38 {
39 public:
40  explicit CPoweredObject(ObjectInterfaceTypes& types)
41  {
42  types[static_cast<int>(ObjectInterfaceType::Powered)] = true;
43  }
44  virtual ~CPoweredObject()
45  {}
46 
48  virtual CObject* GetPower() = 0;
50  virtual void SetPower(CObject* power) = 0;
51 
53  virtual Math::Vector GetPowerPosition() = 0;
55  virtual void SetPowerPosition(const Math::Vector& powerPosition) = 0;
56 };
57 
58 inline float GetObjectEnergy(CObject* object)
59 {
60  float energy = 0.0f;
61 
63  {
64  CObject* power = dynamic_cast<CPoweredObject*>(object)->GetPower();
65  if (power != nullptr && power->Implements(ObjectInterfaceType::PowerContainer))
66  {
67  energy = dynamic_cast<CPowerContainerObject*>(power)->GetEnergy();
68  }
69  }
70 
71  return energy;
72 }
73 
74 inline float GetObjectEnergyLevel(CObject* object)
75 {
76  float energy = 0.0f;
77 
79  {
80  CObject* power = dynamic_cast<CPoweredObject*>(object)->GetPower();
81  if (power != nullptr && power->Implements(ObjectInterfaceType::PowerContainer))
82  {
83  energy = dynamic_cast<CPowerContainerObject*>(power)->GetEnergyLevel();
84  }
85  }
86 
87  return energy;
88 }
89 
90 inline bool ObjectHasPowerCell(CObject* object)
91 {
92  return object->Implements(ObjectInterfaceType::Powered) &&
93  dynamic_cast<CPoweredObject*>(object)->GetPower() != nullptr;
94 }
virtual void SetPowerPosition(const Math::Vector &powerPosition)=0
Sets the relative position of power cell.
Interface for objects powered using power cells.
Definition: powered_object.h:37
Definition: power_container_object.h:28
virtual void SetPower(CObject *power)=0
Sets power cell.
ObjectInterfaceType enum.
bool Implements(ObjectInterfaceType type) const
Check if object implements the given type of interface.
Definition: object.h:97
CObject - base class for all game objects.
virtual Math::Vector GetPowerPosition()=0
Returns the relative position of power cell.
3D (3x1) vector
Definition: vector.h:53
Base class for all 3D in-game objects.
Definition: object.h:63
virtual CObject * GetPower()=0
Returns the power cell.
objects that hold power
object powered with power cell