00001 // ///////////////////////////////////////////////////////////////////////////// 00002 // 00003 // Name: EntityComponent.cpp 00004 // Author: Michael Bartsch (ZCCdark203) 00005 // 00006 // Desc : Abstraction base class for adding new functionalities to 00007 // entities. 00008 // 00009 // License: Copyright (C) 2009 Michael Bartsch and Contributors 00010 // 00011 // This program is free software: you can redistribute it 00012 // and/or modify it under the terms of the zlib/libpng License. 00013 // See main.cpp for conditions of distribution and use. 00014 // 00015 // ///////////////////////////////////////////////////////////////////////////// 00016 00017 // Include files 00018 #include "EntityComponent.h" 00019 00020 00021 // EntityComponent class 00022 // Static variables. 00023 u32 EntityComponent::mIDCount = 0; 00024 00025 // EntityComponent constructor. 00026 EntityComponent::EntityComponent(Entity *parent) 00027 : mRefCount(1) 00028 { 00029 // Get new unique ID. 00030 mID = mIDCount++; 00031 00032 // Check if we got a valid pointer. 00033 if(parent != NULL) 00034 { 00035 if(parent->addComponent(this)) 00036 pParent = parent; 00037 00038 else pParent = NULL; 00039 } 00040 00041 else pParent = NULL; 00042 } 00043 00044 // EntityComponent deconstructor. 00045 EntityComponent::~EntityComponent() 00046 { 00047 } 00048 00049 // AngelScript: Will be used to instanciate objects of this class. 00050 EntityComponent* EntityComponent::refFactory(Entity* parent) 00051 { 00052 return new EntityComponent(parent); 00053 } 00054 00055 // Angelscript: Increases the reference counter. 00056 void EntityComponent::refAdd() 00057 { 00058 mRefCount++; 00059 } 00060 00061 // Angelscript: Decreases the reference counter. 00062 void EntityComponent::refRelease() 00063 { 00064 if(--mRefCount == 0) 00065 { 00066 if(pParent != NULL) 00067 { 00068 if(!pParent->removeComponent(this)) 00069 delete this; 00070 } 00071 00072 else delete this; 00073 } 00074 } 00075 00076 // Gets the ID of the component. 00077 u32 EntityComponent::getID() const 00078 { 00079 return mID; 00080 } 00081 00082 // Sets the name of the component. 00083 void EntityComponent::setName(const std::string &name) 00084 { 00085 mName = name; 00086 } 00087 00088 // Gets the name of the component. 00089 const std::string& EntityComponent::getName() const 00090 { 00091 return mName; 00092 } 00093 00094 // Gets the parent of this component. 00095 const Entity* EntityComponent::getParent() 00096 { 00097 return pParent; 00098 } 00099 00100 00101 // EntityComponent Angelscript binding. 00102 void bindEntityComponent(asIScriptEngine *engine) 00103 { 00104 // Forward declarations. 00105 int r; 00106 00107 // Bind EntityComponent class (and dependencies). 00108 r = engine->RegisterObjectType("EntityComponent", sizeof(EntityComponent), asOBJ_REF); 00109 r = engine->RegisterObjectType("Entity", sizeof(Entity), asOBJ_REF); 00110 00111 // Get EntityComponent base functions. 00112 bindEntityComponentBase<EntityComponent>(engine, "EntityComponent"); 00113 00114 // Set EntityComponent behaviour. 00115 r = engine->RegisterObjectBehaviour("EntityComponent", asBEHAVE_FACTORY, "EntityComponent@ f(Entity @)", 00116 asFUNCTION(EntityComponent::refFactory), asCALL_CDECL); assert(r >= 0); 00117 } 00118 00119 // End of File