\chapter{Classes: the basics}

The basic idea behind classes is to group variables which belong together. For example, \eeClass{Vec2} has the values x and y which together make up a 2D position. Without this class, you would have to declare two separate floats to remember a position.

A software library is, for the most part, a collection of classes that work well together. The library developer provides classes which can be used to make your job easier. It would be hard to imagine a game without positions, so it makes sense that Esenthel engine provides you with a class to store such a position. In a most basic form, this class could look like this:

\begin{code}
class Vec2 {
	float x;
	float y;
}  
\end{code}

When this class is defined, it can be used anywhere in your application:

\begin{code}
Vec2 pos;
Vec2 pos2 = pos; // assigns the values of pos to pos2
pos.x = 3;       // change the x value of pos 
pos.y = pos2.x;  // assign the x value of pos2 to the
				 // y value of pos
\end{code}

In every application except from the small exercises we have done in the previous chapters, you should design your own classes. Almost every aspect of your program should be contained within a class. So it's about time you learn more about them.

\begin{note}
When two or more objects or variables belong together, they should be part of a class.
\end{note}

Please note that there's a difference between a class and an object. In the example above, \eeClass{Vec2} is a class, but pos and pos2 are objects of this class. You cannot use \eeClass{Vec2} as if it were an object, but you can use this class to create one:

\begin{code}
Vec2.x = 3; // wrong!
Vec2 pos;
pos.x = 3; // correct.
\end{code}

\section{Create your own class}
Imagine you need a moving circle in your application. To have a circle move at a fixed speed, you will need a variable to store this speed. It clearly belongs with the circle, but it isn't part of the Circle class provided by Esenthel because not every circle needs to move. So we create our own class:

\begin{code}
class movingCircle {
  Circle c;
  Vec2 speed;
}

// declare an object of this class
movingCirle mc;

// some code in Init()
mc.c.set(0.1, Vec2(-0.4, -0.3));
mc.speed = Vec2(0.3, 0.5);

// some code in Update()
mc.c.pos += mc.speed * Time.d();
\end{code}

Once you have a class \eeClass{movingCirle}, you can create as much moving circles as you like, all able to remember their own speed. \textit{(Although the current class can be improved a lot, which you will learn in a next chapter)}.

\begin{exercise}
\begin{enumerate}
  \item Create a container for the class \eeClass{movingCircle}. Every time you press the space bar an element should be added to the container, on a random position and with a random speed.
	\item Make sure all circles update their position in the Update function. When a circle goes outside of the screen, put it back in the middle.
	\item Expand the class \eeClass{movingCirle} with a color. Every object has a random color, to be used to draw it on the screen.
	\end{enumerate}
\end{exercise}

\begin{note}
More work will be done on this exercise in the next chapter. Save it!
\end{note}
