File: ETNew.cpp

package info (click to toggle)
worlded 0.1.3-7
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,044 kB
  • ctags: 1,570
  • sloc: cpp: 8,093; ansic: 6,198; sh: 3,528; makefile: 419; yacc: 316; sed: 16
file content (179 lines) | stat: -rw-r--r-- 3,454 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// $Id: ETNew.cpp,v 1.12 2003/02/09 18:46:53 mat Exp $

#include "ETNew.h"

#include "Application.h"
#include <sstream>

// FIXME: need to replace this by a more "civilised" way to create short
// FIXME: names..
static int m_CurEntityID = 0;

ETNew::ETNew (World *world) : EntityTool (world), m_Entity (NULL)
{}

ETNew::~ETNew()
{
   if (m_Entity) delete m_Entity;
}

/// Apply tool at (x,z)
void
ETNew::Apply3D(Ark::Vector3 newpos, const Ark::Collision &col)
{
   Entity *ent = GetEntity();

   if (!ent)
      return;

   ent->SetPosition (newpos);
   g_Application->PushCommand(CreateCommand());
   m_World->Update();
}

/// Update or reset tool
void
ETNew::Update()
{
   if (m_Entity) 
      delete m_Entity;
  
   m_Entity = NULL;
}

/// Create a command with a correct state.
Command*
ETNew::CreateCommand()
{

   Entity *ent = m_Entity;

   if (ent)
   {
      Command *cmd = new ECNewDelete (m_World, ent, false);
      m_Entity->Unref();
      m_Entity = NULL;
      return cmd;
   }
   else
      return NULL;
}

Entity *
ETNew::GetEntity()
{
   std::cerr << "getentity \n";
   if (m_Entity == NULL)
   {
      std::cerr << " null\n";
      //std::cerr << m_World << std::endl;
      std::ostringstream of;
      std::string mdl = g_Application->GetUI()->SelectModel ("");

      if (mdl == "")
	 return NULL;

      //std::cerr << m_World << std::endl;
      
      of << m_CurEntityID++;
      of.flush();

      m_Entity = new Entity (m_World);
      m_Entity->m_ShortName = of.str();
      m_Entity->SetTemplate ("Scenery", "");
      m_Entity->SetEntry ("model", mdl);
      m_World->Add (m_Entity);
   }

   return m_Entity;
}

///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
ETDelete::ETDelete (World *world) : EntityTool (world), m_Entity (NULL)
{}

ETDelete::~ETDelete ()
{}
      
void
ETDelete::Apply3D (Ark::Vector3 newpos, const Ark::Collision &col)
{
   if (col.GetType() == Ark::Collision::ENTITY)
   {
      m_Entity = static_cast<Entity*>
				(const_cast<Ark::Entity*>(col.m_Entity));
      m_Entity->Ref();

      g_Application->PushCommand(CreateCommand());
      m_World->Update();
   }
   else
      m_Entity = NULL;
}
      
/// Create a command with a correct state.
Command*
ETDelete::CreateCommand()
{
   if (!m_Entity)
      return 0;

   Command *cmd = new ECNewDelete (m_World, m_Entity, true);
   cmd->Execute();
   
   m_Entity->Unref();
   m_Entity = NULL;
   return cmd;
}

///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////

ECNewDelete::ECNewDelete(World *world, Entity *entity,
			 bool deletecmd) : 
   m_World (world),
   m_Entity (entity),
   m_Delete (deletecmd)
{
   assert (m_Entity != 0);
   m_Entity->Ref();
}

ECNewDelete::~ECNewDelete()
{
   if (m_Entity)
      m_Entity->Unref();
}

/// Return the command type.
std::string
ECNewDelete::Type() const
{
   if (m_Delete)
      return "Add entity";
   else
      return "Remove entity";
}

/// Execute this command.
void
ECNewDelete::Execute()
{
   if (m_Delete == false)
      m_World->Add (m_Entity);
   else
      m_World->Remove (m_Entity);

   m_World->Update();
}

/// Undo the action done by this command.
void
ECNewDelete::Unexecute()
{
   if (m_Delete == false)
      m_World->Remove (m_Entity);
   else
      m_World->Add (m_Entity);
}