Skip to content

Interaction System

Jantoom edited this page Sep 13, 2021 · 3 revisions

Introduction

Entities will become Interactable if they hold a Component that extends the InteractionComponent. This allows other entities to trigger collision and interaction events on them. Players specifically trigger interactions by pressing the E key.

Important classes in source/core/src/main/

Pre-existing game objects

  • PhysicsContactListener: Receives all global Box2D collision events between fixtures. It will find the entities that own them, and trigger events "collision_start" and "collision_end" when collisions start and end.

Relevant entity components

  • PhysicsComponent: Allows for the PhysicsContactListener to fire events
  • ColliderComponent: Responsible for registering a fixture into the physics world. It serves as an easy-to-use interface between higher-level scripting and lower-level physics calculations. A HitboxComponent is one that doesn't block movement of colliding entities.
  • AnimationRenderComponent: Switches between sprite animations for rendering. It is typical for most Interactable objects switching to a highlighted state while colliding with the player.
  • InteractionComponent: Serves as a base class for acting on collisions and interactions. It implements the interface Interactable, which abstracts all methods relevant to inter-entity interaction. These are preCollisionCheck, onCollisionStart, onCollisionEnd and onInteraction. By default, an InteractionComponent:
    • listens for the events "collision_start", "collision_end" and "interaction"
    • contains a reference to the entity's HitboxComponent for checking fixtures
    • contains a reference to the entity's AnimationRenderComponent for switching to highlighted textures
  • SurveyorComponent: Responsible for triggering the "interaction" event. It works by surveying locations on the boundary of a HitboxComponent for existing collisions, and chooses the closest entity collision for interaction.
  • KeyboardPlayerInputComponent: Pressing the E key will activate the SurveyorComponent so that interactions may occur.

UML Diagram

How to use the Interaction System

Entity requirements

  • Player: Must have a PhysicsComponent, HitboxComponent, KeyboardPlayerInputComponent and SurveyorComponent to use the Interaction system
  • Interactable entities: Must have a PhysicsComponent, HitboxComponent and InteractionComponent to use the Interaction system
    • most interactable entities will have an AnimationRenderComponent to toggle textures when colliding, but this isn't compulsory
    • the player can also be an interactable entity

Code practices

  • onCollisionStart and onCollisionEnd should do a pre-collision check on the passed fixtures using the inherited preCollisionCheck implementation.
  • onCollisionStart, onCollisionEnd and onInteraction should check for unique entity components using an "if, else if" statement to determine what functionality should be run.
    @Override
    public void onCollisionStart(Fixture me, Fixture other) {
        Entity target = preCollisionCheck(me, other);
        if (target == null) {
            return;
        } else if (target.getComponent(PlayerActions.class) != null) {
            logger.info("BED ended collision with PLAYER, un-highlighting bed");
            animator.startAnimation("bed");
        }
    }

Design practices

  • Rendered entities are usually highlighted when colliding, and un-highlighted otherwise
  • (To be added) Informational overlays when colliding with important entities

Examples of the Interaction System in-game

Bed

Entity onCollisionStart onCollisionEnd onInteraction
Player Bed is highlighted Bed is un-highlighted Win condition is triggered
Passive Bed Texture Active Bed Texture
Clone this wiki locally