The control scheme

It's important to define a control scheme that allows us to control the character in different ways. Suppose that you want to ship the game both for (desktop) computers and mobile devices. One has a keyboard, the other one has a touchscreen. As a result, you need to write twice how the character should move based on the input. Now, imagine that you add also AI enemies; they need a script that not only makes them think but also move. However, all this repetition of the code can be avoided with a simple control scheme. We abstract the movement in a separate class, which doesn't care how the input arrives, but once it arrives, this class will make the character move. We can call this Movement Component, which we are going to implement in this section.

Have a look at the following figure of the control scheme:

As you can see, the Movement Component can receive input from different sources. These sources don't have to worry about movement since the Movement Component will take care of that. The output of the Movement Component is to tell the physics engine which forces are applied to the character to make it move. Later, we will implement both the Player Controller (for input based on a keyboard) in Chapter 11, Don't Forget to Save, and the UI Controllers (for input based on the touchscreen) in Chapter 12The Nature of Export. Here, in this chapter, we will focus only on the Movement Component.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset