0
\$\begingroup\$

What's the proper way, a proper way, or any proper way at all, to handle interactions between a sprite (usually a player) and another sprite (can be any other object)? (in a tile based world)

i.e. getting into a vehicle, talking with other character, reading signs, etc.

My first instinct is to have some kind of "feeler" or interaction rectangle that's always in front of the player in the direction the player is facing. Then if that rectangle overlaps some kind of intractable object (driver door on vehicle, other character, sign, etc) I would be able to initiate an interaction.

Is this a naive implementation?

EDIT

This is for a 2D engine, written in C# for Xbox Live Arcade and Windows.

\$\endgroup\$
6
  • \$\begingroup\$ "proper way" is defined as "the way that works" - there are of course always different ways to improve upon that, based on requirements or merely personal preference, to achieve better performance, ease of use, number of lines of code, readability, maintainability, backwards compatibility, coding style, etc - though many of these requirements are actually trade-offs against other requirements. This also means there's never a "best way" in case you were wondering. ;) \$\endgroup\$
    – CodeSmile
    Commented Jan 14, 2014 at 13:16
  • \$\begingroup\$ As a helpful note, I think you're on the right track if those interactive objects are not tiles but placed freely in the world. If they were tiles you could simply look up the type of tile one tile ahead of the player. \$\endgroup\$
    – CodeSmile
    Commented Jan 14, 2014 at 13:20
  • \$\begingroup\$ @LearnCocos2D Of course there is a best way! Each program is different, they have different amount of data used, different data structure etc but in the end, for every program there is a best way. The best way saves you memory, computational power, which is a concern of every game that runs on a mobile device. If it is for laptop or desktop computers, these are still extremely important. There is always a best known way. Saying the opposite contradicts with all the research in computer science field. \$\endgroup\$
    – Kogesho
    Commented Jan 14, 2014 at 14:06
  • \$\begingroup\$ The "best way" always depends on requirements. If you optimize for speed, the best way may be to use assembler code. But still it is often subjective and debatable if that specific way is the "best way" even when considering the requirements. Especially when it comes to performance you can write low-level code and still find ways to optimize but the trade-offs are marginal. So when is a way the best way? When you no longer need to improve upon it. Not looking at this from a research point of view but from a practical, pragmatic developer's perspective. \$\endgroup\$
    – CodeSmile
    Commented Jan 14, 2014 at 14:22
  • \$\begingroup\$ @LearnCocos2D I agree, but in this case, we know nothing about his application. \$\endgroup\$
    – Kogesho
    Commented Jan 14, 2014 at 14:25

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .