using System;
using System.
Collections.
Generic;
using System.
Text;
namespace RPGFramework
{ public abstract
class EntityBase
{ Dictionary<Type, EntityRole> roles =
new Dictionary<Type, EntityRole>
();
public virtual EntityRole createRole<TEntityRole>
() where TEntityRole : EntityRole
{ EntityRole role = EntityRole.
create(typeof(TEntityRole
));
addRole<TEntityRole>
(role
);
return role;
} protected virtual void addRole<TEntityRole>
(EntityRole role
) where TEntityRole : EntityRole
{ EntityRole tmpRole;
// Check whether the Entity is already in Role tmpRole = findRole<TEntityRole>
();
bool PlayerInRole =
(tmpRole !=
null);
if(PlayerInRole
) throw new Exception
("Cannot add Role '" + role.
GetType().
Name +
"': Player is already in Role '" + tmpRole.
GetType().
Name +
"'");
// Check wether the Entity is in Base-Type Role Type BaseRole =
typeof(TEntityRole
).
BaseType;
while (BaseRole.
IsAbstract && !
typeof(EntityRole
).
Equals(BaseRole
)) BaseRole = BaseRole.
BaseType;
if (!
typeof(EntityRole
).
Equals(BaseRole
) && !roles.
ContainsKey(BaseRole
)) throw new Exception
("Cannot add Role '" + role.
GetType().
Name +
"': Player is not in required Base-Role '" + BaseRole.
Name +
"'");
List<Type> curRoles = getRoles
();
// Check wether the existing Roles accept the new Role foreach (EntityRole curRole
in roles.
Values) { if(curRole.
acceptAddRole(this, role
) ==
false) { throw new Exception
("Cannot add Role '" + role.
GetType().
Name +
"': existing Role " + curRole.
GetType().
Name +
" does not accept Role '" + role.
GetType().
Name +
"'");
} } // Check wether the new Role accepts the existing Roles if (role.
acceptExistingRoles(this) ==
false) { throw new Exception
("Cannot add Role '" + role.
GetType().
Name +
"': Role '" + role.
GetType().
Name +
"' does not accepts existing Roles or required Role is missing");
} // Delete existing Role(s) from which the new Role derives List<Type> delRoles =
new List<Type>
();
foreach (EntityRole chkRole
in roles.
Values) { //Eigentlich wäre hier Prüfung nötig, ob Rolle gelöscht werden darf - //es werden hier jedoch nur vorhandene Rollen mit davon abgeleiteten //Rollen ersetzt - daher erscheint eine Prüfung nach derzeitigen Kenntnissen nicht nötig if (chkRole.
GetType().
IsAssignableFrom(typeof(TEntityRole
))) { // alle Roles, die für die prüfende Role eine Basisklasse sind, // werden in die delRoles-Liste eingefügt (zum Löschen) role.
initNewRole(chkRole
);
delRoles.
Add(chkRole.
GetType());
} } foreach (Type delRole
in delRoles
) { roles.
Remove(delRole
);
} // Finally add new Role roles
[role.
GetType()] = role;
} protected virtual EntityRole findRole<TEntityRole>
() where TEntityRole : EntityRole
{ Type RoleType =
typeof(TEntityRole
);
foreach (Type role
in roles.
Keys) // Key ist in roles der Type (den wir brauchen) { if (RoleType.
IsAssignableFrom(role
)) { return roles
[role
];
} } return null;
// 'null' repräsentiert in diesem Fall, dass keine Role gefunden wurde } public virtual List<Type> getRoles
() { List<Type> RoleTypes =
new List<Type>
();
foreach (Type RoleType
in roles.
Keys) { RoleTypes.
Add(RoleType
);
} return RoleTypes;
} public virtual bool IsInRole<TEntityRole>
() where TEntityRole : EntityRole
{ return (findRole<TEntityRole>
() !=
null);
} public virtual TEntityRole getRole<TEntityRole>
() where TEntityRole : EntityRole
{ EntityRole curRole = findRole<TEntityRole>
();
if (curRole !=
null) return (TEntityRole
)curRole;
throw new Exception
("Player is not in Role '" +
typeof(TEntityRole
).
Name +
"'");
} }}