freg  0.3
Free-Roaming Elementary Game
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Illuminator.cpp
Go to the documentation of this file.
1  /* freg, Free-Roaming Elementary Game with open and interactive world
2  * Copyright (C) 2012-2014 Alexander 'mmaulwurff' Kromm
3  * mmaulwurff@gmail.com
4  *
5  * This file is part of FREG.
6  *
7  * FREG is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * FREG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with FREG. If not, see <http://www.gnu.org/licenses/>. */
19 
20 #include "blocks/Illuminator.h"
21 #include "BlockManager.h"
22 #include "Inventory.h"
23 #include "World.h"
24 
25 Illuminator::Illuminator(const int kind, const int sub) :
26  Active(kind, sub, NONSTANDARD),
27  fuelLevel(MAX_FUEL),
28  isOn(false)
29 {}
30 
31 Illuminator::Illuminator(QDataStream & str, const int kind, const int sub) :
32  Active(str, kind, sub, NONSTANDARD),
33  fuelLevel(),
34  isOn()
35 {
36  str >> fuelLevel >> isOn;
37 }
38 
40  return (Sub()==GLASS) ? DAMAGE_NO : DAMAGE_HEAT;
41 }
42 
43 QString Illuminator::FullName() const {
44  switch ( Sub() ) {
45  case WOOD: return tr("Torch, fuel: %1" ).arg(fuelLevel);
46  case STONE: return tr("Flint, charges: %1" ).arg(fuelLevel);
47  case GLASS: return tr("Flashlight, battery: %1").arg(fuelLevel);
48  default: return tr("Lantern (%1), fuel: %2" ).
49  arg(SubName(Sub())).arg(fuelLevel);
50  }
51 }
52 
53 Block * Illuminator::DropAfterDamage(bool * const delete_block) {
54  *delete_block = false;
55  Block * const pile = BlockManager::NewBlock(BOX, DIFFERENT);
56  pile->HasInventory()->Get(this);
57  return pile;
58 }
59 
61  if ( Sub() == WOOD || Sub() == STONE ) {
62  fuelLevel = ( fuelLevel >= 10 ) ?
63  fuelLevel - 10 : 0;
64  return USAGE_TYPE_SET_FIRE;
65  } else {
66  isOn = not isOn;
67  Active * const active = user->ActiveBlock();
68  if ( active != nullptr ) {
69  active->UpdateLightRadius();
70  }
71  return USAGE_TYPE_INNER;
72  }
73 }
74 
76  if ( fuelLevel == 0 || not isOn ) return 0;
77  switch ( Sub() ) {
78  default:
79  case STONE: return 0;
80  case WOOD: return 4;
81  case IRON: return 5;
82  case GLASS: return 7;
83  }
84 }
85 
86 int Illuminator::ShouldAct() const { return FREQUENT_RARE; }
88 
90  GetWorld()->Shine(X(), Y(), Z(), LightRadius());
91 }
92 
94  if ( fuelLevel == 0 ) {
95  if ( Sub() == WOOD ) {
96  Break();
97  }
98  } else {
99  if ( Sub() != STONE && isOn ) {
100  --fuelLevel;
101  }
102  }
103  return INNER_ACTION_NONE;
104 }
105 
106 void Illuminator::SaveAttributes(QDataStream & out) const {
108  out << fuelLevel << isOn;
109 }
int X() const
Definition: Active.cpp:158
virtual void SaveAttributes(QDataStream &) const
Definition: Block.cpp:307
virtual void UpdateLightRadius()
Definition: Active.cpp:32
10
Definition: header.h:170
wearable Wearable() const override
Definition: Illuminator.cpp:87
static QString SubName(int sub)
Returns translated substance name.
Definition: Block.cpp:69
Illuminator(int sub, int id)
Definition: Illuminator.cpp:25
short Z() const
Definition: Xyz.cpp:30
void SaveAttributes(QDataStream &out) const override
virtual bool Get(Block *block, int start=0)
Returns true on success.
Definition: Inventory.cpp:78
void DoRareAction() override
Definition: Illuminator.cpp:89
quint16 fuelLevel
Definition: Illuminator.h:47
virtual Inventory * HasInventory()
Definition: Block.cpp:224
Block * DropAfterDamage(bool *delete_block) override
Should return dropped block.
Definition: Illuminator.cpp:53
5
Definition: Block.h:44
9
Definition: header.h:169
void Shine(int x, int y, int z, int level)
If init is false, light will not spread from non-invisible blocks.
inner_actions
See Shred::PhysEventsRare() for details.
Definition: Active.h:36
11
Definition: header.h:171
int Y() const
Definition: Active.cpp:162
Provides declaration for class Inventory for freg.
World * GetWorld() const
Definition: Active.cpp:140
QString FullName() const override
Definition: Illuminator.cpp:43
int Sub() const
Definition: Block.h:144
int LightRadius() const override
Definition: Illuminator.cpp:75
wearable
Definition: Block.h:28
12
Definition: header.h:172
static Block * NewBlock(int kind, int sub)
Use this to receive a pointer to new not-normal block.
Definition: Active.h:46
usage_types
Definition: header.h:196
usage_types Use(Block *) override
Definition: Illuminator.cpp:60
virtual Active * ActiveBlock()
Definition: Block.cpp:226
0
Definition: header.h:160
inner_actions ActInner() override
Definition: Illuminator.cpp:93
16
Definition: header.h:131
int ShouldAct() const override
Definition: Illuminator.cpp:86
void Break()
Set durability to null.
Definition: Block.cpp:230
const int MAX_FUEL
Definition: Illuminator.h:25
int DamageKind() const override
Definition: Illuminator.cpp:39
Block without special physics and attributes.
Definition: Block.h:89
12
Definition: Block.h:51