File: gameobj.cpp

package info (click to toggle)
angelscript 2.35.1%2Bds-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,388 kB
  • sloc: cpp: 71,969; asm: 1,558; makefile: 665; xml: 214; javascript: 42; ansic: 22; python: 22; sh: 7
file content (119 lines) | stat: -rw-r--r-- 2,319 bytes parent folder | download | duplicates (2)
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
#include "gameobj.h"
#include "scriptmgr.h"
#include "gamemgr.h"

using namespace std;

CGameObj::CGameObj(char dispChar, int x, int y)
{
	// The first reference is already counted when the object is created
	refCount         = 1;

	isDead           = false;
	displayCharacter = dispChar;
	this->x          = x;
	this->y          = y;
	controller       = 0;

	weakRefFlag      = 0;
}

CGameObj::~CGameObj()
{
	if( weakRefFlag )
	{
		// Tell the ones that hold weak references that the object is destroyed
		weakRefFlag->Set(true);
		weakRefFlag->Release();
	}

	if( controller )
		controller->Release();
}

asILockableSharedBool *CGameObj::GetWeakRefFlag()
{
	if( !weakRefFlag )
		weakRefFlag = asCreateLockableSharedBool();

	return weakRefFlag;
}

int CGameObj::AddRef()
{
	return ++refCount;
}

int CGameObj::Release()
{
	if( --refCount == 0 )
	{
		delete this;
		return 0;
	}
	return refCount;
}

void CGameObj::DestroyAndRelease()
{
	// Since there might be other object's still referencing this one, we
	// cannot just delete it. Here we will release all other references that
	// this object holds, so it doesn't end up holding circular references.
	if( controller )
	{
		controller->Release();
		controller = 0;
	}

	Release();
}

void CGameObj::OnThink()
{
	// Call the script controller's OnThink method
	if( controller )
		scriptMgr->CallOnThink(controller);
}

bool CGameObj::Move(int dx, int dy)
{
	// Check if it is actually possible to move to the desired position
	int x2 = x + dx;
	if( x2 < 0 || x2 > 9 ) return false;

	int y2 = y + dy;
	if( y2 < 0 || y2 > 9 ) return false;

	// Check with the game manager if another object isn't occupying this spot
	CGameObj *obj = gameMgr->GetGameObjAt(x2, y2);
	if( obj ) return false;

	// Now we can make the move
	x = x2;
	y = y2;

	return true;
}

void CGameObj::Send(CScriptHandle msg, CGameObj *other)
{
	if( other && other->controller )
		scriptMgr->CallOnMessage(other->controller, msg, this);
}

void CGameObj::Kill()
{
	// Just flag the object as dead. The game manager will 
	// do the actual destroying at the end of the frame
	isDead = true;
}

int CGameObj::GetX() const
{
	return x;
}

int CGameObj::GetY() const
{
	return y;
}