Castle Battle  1.0
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
Castle Class Reference

The Castle handles all things related to the Castles Building, treasure, physics and collisions. More...

#include <castle.h>

Public Member Functions

 Castle (scene::ISceneManager *smgr, Physics *physics, IrrlichtDevice *device, video::IVideoDriver *driver, core::vector3df center)
 Default constructor. More...
 
bool buildCastle (core::vector3df center)
 Build the castle. More...
 
core::vector3df getSideSize (char side)
 Get size from a side. More...
 
void setTreasure ()
 Set treasure in the castle. More...
 
core::vector3df calculateAbsoluteCenter ()
 Get center of the castle in Irrlicht world coordinates. More...
 
bool checkTarget ()
 Check if target is moved TARGET_OFFSET is the max offset of the target from initial position. More...
 

Public Attributes

video::SColor COLOR_LIGHT
 
video::SColor COLOR_DARK
 

Private Member Functions

void createBlock (core::vector3df position, core::vector3df scale, core::vector3df rotation, char side, video::SColor color=randomColor())
 Create the block of the castle at a specified position, rotation and scaling. More...
 
void addToPhysicsWorld ()
 

Private Attributes

scene::ISceneManager * smgr
 
video::IVideoDriver * driver
 
core::vector3df position
 
scene::IMeshSceneNode * node
 Last node used. More...
 
IrrlichtDevice * device
 
std::multimap< char, scene::IMeshSceneNode * > nodes
 Associative array for blocks. More...
 
Targettarget
 Target node. More...
 
core::vector3df initialTargetPos
 Initial position of the first block. More...
 
Physicsphysics
 

Detailed Description

The Castle handles all things related to the Castles Building, treasure, physics and collisions.

Constructor & Destructor Documentation

◆ Castle()

Castle::Castle ( scene::ISceneManager *  smgr,
Physics physics,
IrrlichtDevice *  device,
video::IVideoDriver *  driver,
core::vector3df  center 
)

Default constructor.

Given irrlicht and bullet instances, this will initialize colors and start building The color palette of the castle is based of 2 type of color randomly generated. COLOR_LIGHT which represent light colors COLOR_DARK which represent dark colors.

See also
createBlock()
buildCastle()
randomColor()
11  {
12  this->smgr = smgr;
13  this->nodes.clear();
14  this->driver = driver;
15  this->device = device;
16  this->physics = physics;
17  this->position = center;
18  this->COLOR_DARK = randomColor();
19  this->COLOR_LIGHT = randomColor();
20  this->position = center;
21  if(this->buildCastle(this->position)){
22  this->addToPhysicsWorld();
23  this->setTreasure();
24  }
25 }
bool buildCastle(core::vector3df center)
Build the castle.
Definition: castle.cpp:27
video::SColor randomColor()
Get random color.
Definition: bullethelper.cpp:63
video::IVideoDriver * driver
Definition: castle.h:19
void addToPhysicsWorld()
Definition: castle.cpp:136
video::SColor COLOR_DARK
Definition: castle.h:47
video::SColor COLOR_LIGHT
Definition: castle.h:46
IrrlichtDevice * device
Definition: castle.h:23
std::multimap< char, scene::IMeshSceneNode * > nodes
Associative array for blocks.
Definition: castle.h:26
Physics * physics
Definition: castle.h:31
scene::ISceneManager * smgr
Definition: castle.h:18
void setTreasure()
Set treasure in the castle.
Definition: castle.cpp:170
core::vector3df position
Definition: castle.h:20

Member Function Documentation

◆ addToPhysicsWorld()

void Castle::addToPhysicsWorld ( )
private
Deprecated:
Useless method
136  {
137 
138 
139 }

◆ buildCastle()

bool Castle::buildCastle ( core::vector3df  center)

Build the castle.

Given the position, this method will start building the castle from that position. For now it will get data of the castle from xml file in media/Castle

Parameters
centervector3df position from where to start building
Returns
bool true if castle is successfully builded or false if something went wrong
27  {
28 
29  irr::io::IXMLReader* xml = this->device->getFileSystem()->createXMLReader("media/castle/castle.xml"); //create xml reader
30  if (!xml)
31  return false;
32 
33  const stringw sideTag(L"side"); //we'll be looking for this tag in the xml
34  core::stringw currentSection; //keep track of our current section
35  const stringw blockTag(L"block"); //constant for blocktag
36  core::stringc position, lastposition;
37  float width, height, distance = 0.f;
38  // core::vector3d<f32> * edges = new core::vector3d<f32>[8];
39  f32 offsets[4]={0,0,0,0};
40  u32 reading = -1 ;
41  core::vector3df pos;
42  core::aabbox3d<f32> box;
43  core::matrix4 transformer;
44  std::multimap<char, scene::IMeshSceneNode*>::iterator first, last;
45  //while there is more to read
46  log1("Reading XML castle file and building castle");
47  while (xml->read())
48  {
49  //check the node type
50  switch (xml->getNodeType()){
51  //we found a new element
52  case irr::io::EXN_ELEMENT:{
53 
54  //we currently are in the empty or mygame section and find the side tag so we set our current block
55  if (currentSection.empty() && sideTag.equals_ignore_case(xml->getNodeName()))
56  {
57  currentSection = sideTag;
58  position = xml->getAttributeValueSafe(L"position");
59  distance = xml->getAttributeValueAsFloat(L"distance");
60  reading++;
61  // maybe a switch case is better
62 
63  if(!this->nodes.empty() && position[0]){ // assert
64 
65  if(reading == 1) offsets[reading] = BLOCKS_OFFSET+getMeshSize(first->second)[0]/2;
66  if(reading == 2 && last != this->nodes.end()) offsets[reading] = BLOCKS_OFFSET+getMeshSize(last->second)[0]/2;
67  if(reading == 3) offsets[reading] = BLOCKS_OFFSET+getMeshSize(first->second)[0]/2;
68  }
69 
70  } else if (currentSection.equals_ignore_case(sideTag) && blockTag.equals_ignore_case(xml->getNodeName())){
71  //read in the key
72  core::vector3df scale = core::vector3df(1.f,1.f,1.f);
73  width = xml->getAttributeValueAsFloat(L"width");
74  height = xml->getAttributeValueAsFloat(L"height");
75  scale = core::vector3df(width,height,1);
76 
77  switch(position[0]){
78  case 'f':
79  pos = core::vector3df(
80  center.X + offsets[0] +scale.X/2, scale.Y/2 , center.Z );
81  this->createBlock(pos,scale, core::vector3df(0,0,0), 'f', this->COLOR_LIGHT);
82  offsets[0] += (width+BLOCKS_OFFSET);
83  break;
84  case 'l':
85  pos = core::vector3df(
86  center.X+scale.X/2, scale.Y/2 , scale.X/2+center.Z+offsets[1]);
87  this->createBlock(pos,scale,core::vector3df(0,90,0), 'l', this->COLOR_DARK);
88  offsets[1] += (width+BLOCKS_OFFSET);
89  break;
90  case 'b':
91  pos = core::vector3df(
92  center.X+scale.X/2+offsets[2], scale.Y/2,
93  center.Z+distance+getMeshSize(this->node)[0]+BLOCKS_OFFSET*5
94  );
95  this->createBlock(pos,scale,core::vector3df(0,0,0), 'b', this->COLOR_LIGHT);
96  offsets[2] += (width+BLOCKS_OFFSET);
97 
98 
99 
100  break;
101  case 'r':
102  pos = core::vector3df(
103  center.X+distance-getMeshSize(this->node)[2], scale.Y/2 , scale.X/2+center.Z+offsets[3]);
104  this->createBlock(pos,scale,core::vector3df(0,90,0), 'r', this->COLOR_DARK);
105  offsets[3] += (width+BLOCKS_OFFSET);
106 
107 
108  break;
109  default:break;
110  }
111  if(!this->nodes.empty()){
112  first = this->nodes.begin();
113  last = this->nodes.end();
114  }
115 
116  }
117 
118  }
119  break;
120 
121  // found the end of an element(side)
122  case irr::io::EXN_ELEMENT_END:
123  if(currentSection.equals_ignore_case(xml->getNodeName())) {position = ""; currentSection = ""; };
124  break;
125 
126  default: break;
127  }
128  }
129 
130  // delete the xml reader
131  xml->drop();
132  return true;
133 
134 }
const f32 BLOCKS_OFFSET
Definition: castle.h:13
video::SColor COLOR_DARK
Definition: castle.h:47
video::SColor COLOR_LIGHT
Definition: castle.h:46
#define log1(_Message)
Log message of 1 level verbosity.
Definition: Logger.h:28
void createBlock(core::vector3df position, core::vector3df scale, core::vector3df rotation, char side, video::SColor color=randomColor())
Create the block of the castle at a specified position, rotation and scaling.
Definition: castle.cpp:140
IrrlichtDevice * device
Definition: castle.h:23
f32 * getMeshSize(scene::ISceneNode *mesh)
Get a size of a 3D Irrlicth mesh.
Definition: bullethelper.cpp:47
std::multimap< char, scene::IMeshSceneNode * > nodes
Associative array for blocks.
Definition: castle.h:26
scene::IMeshSceneNode * node
Last node used.
Definition: castle.h:22
core::vector3df position
Definition: castle.h:20

◆ calculateAbsoluteCenter()

core::vector3df Castle::calculateAbsoluteCenter ( )

Get center of the castle in Irrlicht world coordinates.

179  {
180  core::vector3df relativeCenter = this->position;
181  relativeCenter.X+= this->getSideSize('f').X * 0.5f;
182  relativeCenter.Z+= this->getSideSize('l').X * 0.5f;
183  return relativeCenter;
184 }
core::vector3df getSideSize(char side)
Get size from a side.
Definition: castle.cpp:156
core::vector3df position
Definition: castle.h:20

◆ checkTarget()

bool Castle::checkTarget ( )

Check if target is moved TARGET_OFFSET is the max offset of the target from initial position.

That means if it will be hitted, if the actual position is greater than previos+targetoffset, the player lose I didn't have so much time to build a more sophisticated system, sry!

185  {
186  core::vector3df diff = (this->target->getIrrNode()->getAbsolutePosition() - this->initialTargetPos);
187  diff = core::vector3df(abs(diff.X),abs(diff.Y),abs(diff.Z));
188  return (diff.X >= TARGET_OFFSET || diff.Z >= TARGET_OFFSET);
189 }
core::vector3df initialTargetPos
Initial position of the first block.
Definition: castle.h:30
const f32 TARGET_OFFSET
Definition: castle.h:14
scene::IMeshSceneNode * getIrrNode()
Get Irrlicht node.
Definition: target.cpp:22
Target * target
Target node.
Definition: castle.h:28

◆ createBlock()

void Castle::createBlock ( core::vector3df  position,
core::vector3df  scale,
core::vector3df  rotation,
char  side,
video::SColor  color = randomColor() 
)
private

Create the block of the castle at a specified position, rotation and scaling.

Parameters
positionvector3df position
scalevector3df extent of the block
rotationvector3df rotation of the block
sidechar the side of the block
colorSColor color of the block. Optional.
140  {
141  this->node = this->smgr->addCubeSceneNode(1,0,-1);
142  this->node->setPosition(position);
143  this->node->setScale(scale);
144  this->node->setMaterialFlag(video::EMF_LIGHTING,false);
145  this->smgr->getMeshManipulator()->setVertexColors(this->node->getMesh(),color);
146  logVector(2,"Position castle block at", position);
147  #if (DEBUG_OUTPUT_MASK & 2)
148  this->node->setDebugDataVisible((scene::E_DEBUG_SCENE_TYPE)scene::EDS_BBOX_BUFFERS);
149  #endif
150  assert(this->node != 0);
151  if(this->node){
152  this->nodes.insert(std::make_pair(side, this->node));
153  this->physics->createCastleBlock(this->node,rotation,scale,position);
154  }
155 }
#define logVector(level, _Message, vector)
Log a core::vector3d irrlicht object.
Definition: Logger.h:42
std::multimap< char, scene::IMeshSceneNode * > nodes
Associative array for blocks.
Definition: castle.h:26
Physics * physics
Definition: castle.h:31
scene::ISceneManager * smgr
Definition: castle.h:18
btRigidBody * createCastleBlock(scene::ISceneNode *node, core::vector3df rotation, core::vector3df scale, core::vector3df position)
Create a castle block.
Definition: physics.cpp:90
scene::IMeshSceneNode * node
Last node used.
Definition: castle.h:22
core::vector3df position
Definition: castle.h:20

◆ getSideSize()

core::vector3df Castle::getSideSize ( char  side)

Get size from a side.

There is a problem: left/right side blocks scale is defined with X and a rotation(90°). So Z is 1. Whenever you call this function you get the width(X) not the actual size based on the irrlicht axis and the overall bounding box. Height is correct. So this will return the sum of the width attribute in xml of a specified side. (f = front, b=back, l=left, r=right )

Parameters
sidechar letter of the side (f = front, b=back, l=left, r=right )
Returns
vector3df
156  {
157  core::vector3df size_side = core::vector3df(0,0,0) ;
158  std::pair<
159  std::multimap<char, scene::IMeshSceneNode*>::iterator,
160  std::multimap<char, scene::IMeshSceneNode*>::iterator
161  > blocks;
162  blocks = this->nodes.equal_range(side);
163  for(std::multimap<char, scene::IMeshSceneNode*>::iterator it = blocks.first; it != blocks.second; ++it){
164  size_side.X+= it->second->getScale().X;
165  size_side.Y+= it->second->getScale().Y;
166  size_side.Z+= it->second->getScale().Z;
167  }
168  return size_side;
169 }
std::multimap< char, scene::IMeshSceneNode * > nodes
Associative array for blocks.
Definition: castle.h:26

◆ setTreasure()

void Castle::setTreasure ( )

Set treasure in the castle.

170  {
171 // core::vector3df position = this->calculateAbsoluteCenter();
172  f32 x = this->getSideSize('f').X * getRand(100)/150;
173  f32 z = this->getSideSize('l').X * getRand(100)/100;
174  core::vector3df position = core::vector3df(0.5f+this->position.X + x,0,this->position.Z + z);
175  this->target = new Target(position,this->smgr,this->driver,this->physics);
176  this->initialTargetPos = position;
177  log1("Treasure positioned");
178 }
Target represent the treasure of the castle.
Definition: target.h:7
video::IVideoDriver * driver
Definition: castle.h:19
f32 getRand(f32 maxValue, f32 offset)
Get a random value.
Definition: bullethelper.cpp:44
#define log1(_Message)
Log message of 1 level verbosity.
Definition: Logger.h:28
core::vector3df initialTargetPos
Initial position of the first block.
Definition: castle.h:30
Physics * physics
Definition: castle.h:31
scene::ISceneManager * smgr
Definition: castle.h:18
core::vector3df getSideSize(char side)
Get size from a side.
Definition: castle.cpp:156
core::vector3df position
Definition: castle.h:20
Target * target
Target node.
Definition: castle.h:28

Member Data Documentation

◆ COLOR_DARK

video::SColor Castle::COLOR_DARK

◆ COLOR_LIGHT

video::SColor Castle::COLOR_LIGHT

◆ device

IrrlichtDevice* Castle::device
private

◆ driver

video::IVideoDriver* Castle::driver
private

◆ initialTargetPos

core::vector3df Castle::initialTargetPos
private

Initial position of the first block.

◆ node

scene::IMeshSceneNode* Castle::node
private

Last node used.

◆ nodes

std::multimap<char, scene::IMeshSceneNode*> Castle::nodes
private

Associative array for blocks.

This memorize of each side f, l, r, b, the nodes of the blocks

◆ physics

Physics* Castle::physics
private

◆ position

core::vector3df Castle::position
private

◆ smgr

scene::ISceneManager* Castle::smgr
private

◆ target

Target* Castle::target
private

Target node.


The documentation for this class was generated from the following files: