Skip to main content
changed for 2d game
Source Link
Kogesho
  • 227
  • 2
  • 5

One way of doing this (I am updating my answer for a 2D program):

You can have a key assigned to use objects. Let's say this key is 'E'

You constantly check which object is in the middle of the screen (ray casting or color picking)

When user presses key 'E' IfCheck the object is close enoughposition of your character and has a way to be used by the userdirection it looks at. Add them up, if there is an object at that position You call the Use function of the object

Inheritance or interface can help you here. The top level GeneralObject interface has the use function unimplemented, and your vehicle class can have a use function that puts the user in it, the door class which implements opens the door when use function is used, or character class which starts talking when its use function is called. If you press 'E' when a non-interactable object is in front of player, than nothing happens because that object's class's use function is empty.

This way, you don't have to say:

If ( objectToInteract is door)
   do this....
If ( objectToInteract is character)
   do this...

Instead, you say

objects.at(pickedobjectindex).use()

And the object will do what it should do, if it should somehow react.

One way of doing this:

You can have a key assigned to use objects. Let's say this key is 'E'

You constantly check which object is in the middle of the screen (ray casting or color picking)

When user presses key 'E' If the object is close enough and has a way to be used by the user You call the Use function of the object

Inheritance or interface can help you here. The top level GeneralObject interface has the use function unimplemented, and your vehicle class can have a use function that puts the user in it, the door class which implements opens the door when use function is used, or character class which starts talking when its use function is called. If you press 'E' when a non-interactable object is in front of player, than nothing happens because that object's class's use function is empty.

This way, you don't have to say:

If ( objectToInteract is door)
   do this....
If ( objectToInteract is character)
   do this...

Instead, you say

objects.at(pickedobjectindex).use()

And the object will do what it should do, if it should somehow react.

One way of doing this (I am updating my answer for a 2D program):

You can have a key assigned to use objects. Let's say this key is 'E'

When user presses key 'E' Check the position of your character and the direction it looks at. Add them up, if there is an object at that position You call the Use function of the object

Inheritance or interface can help you here. The top level GeneralObject interface has the use function unimplemented, and your vehicle class can have a use function that puts the user in it, the door class which implements opens the door when use function is used, or character class which starts talking when its use function is called. If you press 'E' when a non-interactable object is in front of player, than nothing happens because that object's class's use function is empty.

This way, you don't have to say:

If ( objectToInteract is door)
   do this....
If ( objectToInteract is character)
   do this...

Instead, you say

objects.at(pickedobjectindex).use()

And the object will do what it should do, if it should somehow react.

Source Link
Kogesho
  • 227
  • 2
  • 5

One way of doing this:

You can have a key assigned to use objects. Let's say this key is 'E'

You constantly check which object is in the middle of the screen (ray casting or color picking)

When user presses key 'E' If the object is close enough and has a way to be used by the user You call the Use function of the object

Inheritance or interface can help you here. The top level GeneralObject interface has the use function unimplemented, and your vehicle class can have a use function that puts the user in it, the door class which implements opens the door when use function is used, or character class which starts talking when its use function is called. If you press 'E' when a non-interactable object is in front of player, than nothing happens because that object's class's use function is empty.

This way, you don't have to say:

If ( objectToInteract is door)
   do this....
If ( objectToInteract is character)
   do this...

Instead, you say

objects.at(pickedobjectindex).use()

And the object will do what it should do, if it should somehow react.