Gate Entity Design - UQdeco2800/2022-studio-3 GitHub Wiki

Introduction

It was desired to have the city fully enclosed by walls, to further add to the feel that you were defending a great city. Unfortunately this introduced a new problem, as having walls completely around a city would block the Friendly entities from escaping. In order to circumvent this issue, the Gate entity was created, in conjunction with the newly created GateCollider.java component. An entity with a GateCollider has a variable collider that blocks all entities from passing except those with a FriendlyComponent attached. This allows for the desired functionality of enemies being unable to access the city, while allies can leave. It is to be noted, that when the gate is open, ALL entities can enter, not just allies - this is intended functionality: be careful sending out troops if enemies are nearby, they may infiltrate the city!

GateCollider.java Component

The GateCollider component implements this logic as follows:

  • It listens for the onCollisionStart and onCollision end events

  • If it collides with an Entity, it determines if the entity has a friendlyComponent() -> if not do nothing

  • If the friendly entity colliding with the gate is the first to collide with it, open the gate (Change the texture, disable the collider and play the aniumation)

  • If the friendly entity colliding with the gate isn't the only entity colliding with the gate do nothing (if the gate is open, don't open it again)

  • OnCollisionEnd, check if the entity is friendly. If there are no more friendlies in contact with the gate, close it

  • To close the gate, simply play the closing animation, and re-enable the collider

Creating a gate

  • An example of a gate creation exists in BuildingFactory, in the functions createNSGate() and createEWGate()

How to allow an Entity to pass through the gate

To allow an Entity to have access to the city through the gates, you must add a FriendlyComponent to it in its respective creation factory e.g

Entity myEntity = new Entity();
myEntity.addComponent(new FriendlyComponent())

This entity will now have access to the gates

Testing

To visually verify this gate is working, the following may be checked:

  1. 4 gates spawn, each in the centre of an edge of the city
  2. When moving an allied unit (e.g Forager) to the gate, it will play an opening animation
  3. Once open, allies may pass through
  4. When an allied unit leaves the gate's radius, it will close
  5. At any time while the gate is closed, no non-friendly may pass through it