The problem
Suppose I have a game where there are multiple Input Controllers:
LocalPlayerController
: Reads inputs from the keyboard/mouse/gamepad.RemotePlayerController
: Reads input from the network packets.AiController
: Takes decisions based on the environment.
My Character
entitity could be a human player, or an NPC. It currently exposes methods like moveForward
, moveBack
, strafeLeft
, strafeRight
.
Those methods are called by each controller acordingly:
class LocalPlayerController {
public void update(Character character) {
if (Input.getKeyDown(W)) character.moveForward();
if (Input.getKeyDown(S)) character.moveBack();
if (Input.getKeyDown(A)) character.strafeLeft();
if (Input.getKeyDown(A)) character.strafeRight();
}
}
The same for the other kind of controllers:
class RemotePlayerController {
public void update(Character character) {
// Read network packets and call those 4 methods
}
}
class AiController {
public void update(Character character) {
// Makes a decision based on the environment and call those 4 methods
}
}
The question
I know I can abstract the character movement (moveForward()
, moveLeft()
, etc) and interaction (shoot()
, openDoor(door)
, etc).
Question: What would be the best approach to abstract the eyes/head looking direction?
Options:
- Option 1: Have methods/properties that move the head direction based on yaw/pitch. For example:
setYaw(yaw)
,setPitch(pitch)
,getYaw()
andgetPitch()
. Inside each setter, they would limit the pitch and yaw to a maximum per game tick likedelta_yaw = max(delta_yaw, MAX_DELTA_YAW)
- Option 2: Have methods/properties that set the look at target in world space where the head should look at. For example:
setHeadLookAt(target)
,getHeadLokAt()
. Inside the class would store the current look at target xyz and rotate towards that point acording to aMAX_DELTA_YAW
,MAX_DELTA_PITCH
, etc. - Option 3: Another different approach?
The first option seems suited for a LocalPlayerController
because that's the naive way you normally read the mouse coordinates in the screen and adjust the deltaYaw and deltaPitch based on the deltaX and deltaY of the mouse.
The second option seems more suited for an AiController
because normally you want your Ai to look to a certain target.
What would it be the best option? Please justify your answer with examples of pros and cons of each option acording to different input methods (mouse, AI, network).
- Related question: Decoupling the entity from whatever controls it