Skip to content

Commit 6b6b57a

Browse files
committed
fixed up previous tutorials
1 parent b10d77e commit 6b6b57a

File tree

5 files changed

+251
-112
lines changed

5 files changed

+251
-112
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
_site
22
.sass-cache
33
.jekyll-metadata
4+
Gemfile
5+
Gemfile.lock

tutorials/_posts/2016-04-08-foreword.md

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ number: 0
1010
>
1111
> * An overview of what the tutorial series will be all about;
1212
> * A description of the game we're building during this tutorial series;
13-
> * Information about how to set up your development environment.
13+
> * Information about how to set up your development environment.
1414
1515
### Tutorial Series
1616

@@ -23,14 +23,14 @@ our way up to a fully-playable game. We will cover topics as:
2323
* Adding **sounds effects** and **ambiance music** to the game
2424
* And probably much more!
2525

26-
In case something wasn't covered that you would very much like explained, drop us a comment at
27-
[Gitter](https://gitter.im/EngoEngine/engo), or [create an issue at
26+
In case something wasn't covered that you would very much like explained, drop us a comment at
27+
[Gitter](https://gitter.im/EngoEngine/engo), or [create an issue at
2828
GitHub](https://github.com/EngoEngine/engoengine.github.io/issues/new) to request improvements of this tutorial series.
2929

3030
### The Game
31-
The game we will be building, looks a lot like [Mini Metro](http://store.steampowered.com/app/287980/). In that game,
31+
The game we will be building, looks a lot like [Mini Metro](http://store.steampowered.com/app/287980/). In that game,
3232
cities are popping up which you will have to connect with metro lines. More and more people will want to travel from
33-
various cities, making the game harder and harder, until the travelers have to wait too long and it's game over.
33+
various cities, making the game harder and harder, until the travelers have to wait too long and it's game over.
3434

3535
In our game, we won't be connecting travelers and cities with metro lines. Instead, we're building highways. This has
3636
quite some interesting features:
@@ -39,49 +39,47 @@ quite some interesting features:
3939
* You may want to add public transportation, i.e. buses, for them to use instead of the car;
4040
* Trucks and vans might slow down the transportation network, as they usually tend to drive at a slower pace;
4141
* Make up your own laws when it comes to taxes (gas, mileage, etc.), but also when it comes to how fast different
42-
types of vehicles can drive on various lanes.
42+
types of vehicles can drive on various lanes.
4343
* You can connect two highways and get an intersection; there's no need for this to happen in cities.
4444

4545
One can think of much more interesting features, and we could fill a whole page just listing ideas, but for now, let's
46-
teach you how to set up your own development environment.
46+
teach you how to set up your own development environment.
4747

4848
### Development Environment
49-
For the moment, you can use Windows, Linux and OSX. Windows is the poorest-supported of those
50-
at the moment (missing [audio support](https://github.com/EngoEngine/engo/issues/174)) - but that's something we are
51-
working on.
49+
For the moment, you can use Windows, Linux and OSX.
5250

5351
> #### Build-Time Dependencies
54-
> * Linux: `sudo apt-get install libopenal-dev libglu1-mesa-dev freeglut3-dev mesa-common-dev xorg-dev libgl1-mesa-dev`
52+
> * Linux: `sudo apt-get install libasound2-dev libglu1-mesa-dev freeglut3-dev mesa-common-dev xorg-dev libgl1-mesa-dev git-all`
5553
> * Windows: If you're running on Windows you'll need a gcc compiler that the go tool can use and have `gcc.exe` in your PATH environmental variable. We recommend [Mingw](http://mingw-w64.org/doku.php/start) since it has been tested.
56-
> * OSX: No known dependencies.
54+
> * OSX: No additional dependencies.
5755
>
5856
> Note that you will only need these installed when you *build* the game; you will not need these when you want to
5957
> *run* the game. You will probably need the 'runtime' version of those libraries though (instead of the `-dev` ones),
60-
> if you want to run the game.
61-
62-
In any case, you will need to [install the latest version of Go](https://golang.org/dl/). Make sure you
58+
> if you want to run the game.
59+
60+
In any case, you will need to [install the latest version of Go](https://golang.org/dl/). Make sure you
6361
correctly set your `$GOROOT` (which should point to the installation directory of Go, and your `$GOPATH`, which is
64-
basically the root of all your Go projects. Both of these are
65-
[environment variables](https://en.wikipedia.org/wiki/Environment_variable).
62+
basically the root of all your Go projects. Both of these are
63+
[environment variables](https://en.wikipedia.org/wiki/Environment_variable).
6664

6765
Within this `$GOPATH` directory, you will want to create a directory called `src`. What you want to do now, is open
6866
up a terminal / command prompt, go to this location (using the `cd` command), and then type in the following Go command:
6967
`go get -u engo.io/engo`
7068

71-
This command will fetch `engo` from the repository, including all its dependencies.
69+
This command will fetch `engo` from the repository, including all its dependencies.
7270

7371
Now, you will want to create your own project directory for your own game. In Go, it is customary to place the source code
74-
of these projects within code repositories, such as GitHub and Bitbucket. The location of these
72+
of these projects within code repositories, such as GitHub and Bitbucket. The location of these
7573
repositories is the "import path" of such a Go project. We should create this directory structure within the `src`
76-
folder of the `$GOPATH`.
74+
folder of the `$GOPATH`.
7775

7876
> #### Example
79-
> Our GitHub username is `EngoEngine`, and our project is going to be called `TrafficManager`. We are hosting the repository on
80-
> GitHub. This means we want to ensure that this path exists: `$GOPATH/src/github.com/EngoEngine/TrafficManager`.
77+
> Our GitHub username is `EngoEngine`, and our project is going to be called `TrafficManager`. We are hosting the repository on
78+
> GitHub. This means we want to ensure that this path exists: `$GOPATH/src/github.com/EngoEngine/TrafficManager`.
8179
82-
Within this directory, we will be creating our game. All our files will be within this directory, and we will be
80+
Within this directory, we will be creating our game. All our files will be within this directory, and we will be
8381
executing all `go`-commands (such as `go build` and `go run`) from this directory (in other words: this directory
84-
is our working directory).
82+
is our working directory).
8583

8684
<div class="button-group stacked">
8785
<a class="button" href="/tutorials/01-hello-world">Continue to Tutorial 1: <i>Hello World</i> &raquo;</a>

tutorials/_posts/2016-04-10-first-system.md

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,34 @@ number: 2
66
---
77

88
In this tutorial, we will create our first `System` in `engo`. We will also be using basic input methods such as the
9-
**mouse and keyboard**.
9+
**mouse and keyboard**.
1010

1111
> #### Recap
1212
> Remember what we did in [the last tutorial](/tutorials/01-hello-world)? <br>
13-
> We created a `Scene` which renders a huge *City* icon onto the screen.
13+
> We created a `Scene` which renders a huge *City* icon onto the screen.
1414
1515
> #### Final Code
16-
> The final code **for tutorial 2** is available
17-
> [here at GitHub](https://github.com/EngoEngine/TrafficManager/tree/02-first-system).
16+
> The final code **for tutorial 2** is available
17+
> [here at GitHub](https://github.com/EngoEngine/TrafficManager/tree/02-first-system).
1818
1919
### Step 1: The Concept
2020
Before creating any `System`, it is often good to ask yourself: what is **the purpose** (or task) of this `System`? As
2121
we are building a traffic-managing game which has multiple cities, it would make sense to create a system which creates
22-
new cities.
22+
new cities.
2323

2424
To keep it simple for the sake of this tutorial, we shall create a `System` which creates a new *City* at the location
25-
of the cursor, whenever someone presses **F1**.
25+
of the cursor, whenever someone presses **F1**.
2626

2727
### Step 2: Defining and Adding our System
2828

2929
#### Defining the System
3030

31-
Before adding any functions, let's start by creating a dummy `System` and adding it to our game.
31+
Before adding any functions, let's start by creating a dummy `System` and adding it to our game.
3232

3333
A `System` is anything that implements all these functions:
3434
{% highlight go %}
3535
// System is an interface which implements an ECS-System. A System
36-
// should iterate over its Entities on `Update`, in any way
36+
// should iterate over its Entities on `Update`, in any way
3737
// suitable for the current implementation.
3838
type System interface {
3939
// Update is ran every frame, with `dt` being the time
@@ -59,36 +59,36 @@ func (*CityBuildingSystem) Update(dt float32) {}
5959

6060
{% endhighlight %}
6161

62-
Our `CityBuildingSystem` builds *Cities* on top of the cursor. This requires use of the keyboard and mouse. At first,
62+
Our `CityBuildingSystem` builds *Cities* on top of the cursor. This requires use of the keyboard and mouse. At first,
6363
we don't need to worry about entities, so we'll ignore the `Remove` function for now. We haven't added any entities,
6464
so we can safely ignore removing them.
6565

6666
Let's keep **code quality** in mind, and create a *separate package* for our Systems, called `systems`. This means we create
67-
the directory `$GOPATH/github.com/EngoEngine/TrafficManager/systems`. Within this, we create a new file, with the
67+
the directory `$GOPATH/github.com/EngoEngine/TrafficManager/systems`. Within this, we create a new file, with the
6868
exact contents of the `CityBuildingSystem` described above. However, we do add `package systems` to the top of the file.
6969

7070
#### Adding the System
7171
Now that we've created the `System`, let's add it to our game. The usual way to add `System`s to a game, is by doing
7272
it within the `Setup` function of the `Scene` you're going to use. Since we don't want this to interfere with the
73-
big *City* icon we created in the previous tutorial, we are not only going to add the `System`, but also remove
74-
the `Entity` we created in the last tutorial.
73+
big *City* icon we created in the previous tutorial, we are not only going to add the `System`, but also remove
74+
the `Entity` we created in the last tutorial.
7575

7676
{% highlight go %}
77-
// Setup is called before the main loop starts. It allows you
77+
// Setup is called before the main loop starts. It allows you
7878
// to add entities and systems to your Scene.
7979
func (*myScene) Setup(world *ecs.World) {
8080
common.SetBackground(color.White)
8181
world.AddSystem(&common.RenderSystem{})
82-
82+
8383
world.AddSystem(&systems.CityBuildingSystem{})
8484
}
8585
{% endhighlight %}
8686

8787
> ##### How do we know it works?
8888
> At the moment, we can't be sure it actually works, right?
8989
> Each system can optionally implement `New(*ecs.World)`, which will be called whenever the System is added to the
90-
> scene.
91-
>
90+
> scene.
91+
>
9292
> Let's create a `New` function for our `CityBuildingSystem` like this:
9393
> {% highlight go %}
9494
// New is the initialisation of the System
@@ -98,12 +98,12 @@ func (*CityBuildingSystem) New(*ecs.World) {
9898
{% endhighlight %}
9999

100100
> When we now run our game, we can see the white background, no *City* icon (because we removed it), and the
101-
> console outputs *"CityBuildingSystem was added to the Scene"*.
101+
> console outputs *"CityBuildingSystem was added to the Scene"*.
102102
103103
### Step 3: Figuring out when **F1** is pressed
104-
We want to spawn a *City* whenever someone presses **F1**, so it makes sense that we want to know *when* this happens.
104+
We want to spawn a *City* whenever someone presses **F1**, so it makes sense that we want to know *when* this happens.
105105

106-
First, we need to tell the Engo to listen for the F1 key press. We'll do this by using the
106+
First, we need to tell the Engo to listen for the F1 key press. We'll do this by using the
107107
`engo.Input.RegisterButton(name string, keys Key...)` function. Multiple keys can be be assigned to one identifier.
108108

109109
Add the following line to the `Setup` function for your `Scene`.
@@ -112,7 +112,7 @@ func (*myScene) Setup(world *ecs.World) {
112112
engo.Input.RegisterButton("AddCity", engo.KeyF1)
113113
common.SetBackground(color.White)
114114
world.AddSystem(&common.RenderSystem{})
115-
115+
116116
world.AddSystem(&systems.CityBuildingSystem{})
117117
}
118118
{% endhighlight %}
@@ -121,14 +121,14 @@ We will check "did the gamer press **F1**", on every frame. The `Update` functio
121121
gets called every frame by the `World`. So our checking-code needs to be written there. Engo has a neat feature which
122122
allows you to lookup the state of keys, such as:
123123

124-
* `Down()`: when the button is pressed; will be true as long as the user holds the button,
125-
* `JustPressed()`: when the button was just pressed; will be true for one frame, and cannot become true again unless the
126-
button was released first,
124+
* `Down()`: when the button is pressed; will be true as long as the user holds the button,
125+
* `JustPressed()`: when the button was just pressed; will be true for one frame, and cannot become true again unless the
126+
button was released first,
127127
* `JustReleased()`: same as `JustPressed()`, but with releasing the button instead of pressing it,
128128

129129
We don't want to risk placing two cities on top of each other in a very short time period (it's very likely that the
130130
key is pressed longer than one frame, because a frame usually lasts between 16.6ms and 6.94ms). Therefore, we shall use
131-
the `JustPressed()` function.
131+
the `JustPressed()` function.
132132

133133
{% highlight go %}
134134
// Update is ran every frame, with `dt` being the time
@@ -142,12 +142,12 @@ func (*CityBuildingSystem) Update(dt float32) {
142142

143143
> #### How do we know it works?
144144
> Let's run the game, and press **F1** and find out! It should print "The gamer pressed **F1**" just once, when we
145-
> hold the F1-key. It should print it twice when we double-tap it. It shouldn't print it as long as we don't press F1.
145+
> hold the F1-key. It should print it twice when we double-tap it. It shouldn't print it as long as we don't press F1.
146146
147147
### Spawning a *City*
148-
Now we know *when* to spawn a *City*, we can actually write the code to do so.
148+
Now we know *when* to spawn a *City*, we can actually write the code to do so.
149149

150-
Remember the code we used for the large *City*-icon we removed?
150+
Remember the code we used for the large *City*-icon we removed?
151151
{% highlight go %}
152152
city := City{BasicEntity: ecs.NewBasic()}
153153

@@ -177,13 +177,13 @@ for _, system := range world.Systems() {
177177

178178
There are a few things we want to change, before using this in our `CityBuildingSystem`:
179179

180-
* The size: 303 by 641 is a bit too big. Let's change this to 30 by 64.
180+
* The size: 303 by 641 is a bit too big. Let's change this to 30 by 64.
181181
* The location: we want to spawn it at the location of the cursor
182182
* The `world` is unknown in the `Update` function, so we have to work around that
183183

184184
#### The size
185-
As stated, we can easily change the size, by changing the numbers. However, changing the values at the
186-
`SpaceComponent` isn't enough. The texture is still way too big; we can change this by setting the scale to `0.1`
185+
As stated, we can easily change the size, by changing the numbers. However, changing the values at the
186+
`SpaceComponent` isn't enough. The texture is still way too big; we can change this by setting the scale to `0.1`
187187
instead of `1`:
188188

189189
{% highlight go %}
@@ -206,8 +206,8 @@ city.RenderComponent = common.NewRenderComponent(
206206

207207
#### The location
208208
In order to spawn them at the correct location, we need to know where the cursor is. Our first guess might be to use
209-
the `common.Mouse` struct which is available. However, this one returns the actual `(X, Y)` location relative to the
210-
screen size, not the in-game grid system. We have a special `MouseSystem` available for just that.
209+
the `engo.Input.Mouse` struct which is available. However, this one returns the actual `(X, Y)` location relative to the
210+
screen size, not the in-game grid system. We have a special `MouseSystem` available for just that.
211211

212212
##### The `MouseSystem`
213213
The first thing you want to do, is add the `MouseSystem` to your `Scene`:
@@ -220,7 +220,7 @@ func (*myGame) Setup(world *ecs.World) {
220220

221221
world.AddSystem(&common.RenderSystem{})
222222
world.AddSystem(&common.MouseSystem{})
223-
223+
224224
world.AddSystem(&systems.CityBuildingSystem{})
225225
}
226226
{% endhighlight %}
@@ -229,8 +229,8 @@ func (*myGame) Setup(world *ecs.World) {
229229
upon, are already initialized when we're initializing the `CityBuildingSystem`.*
230230

231231
The `MouseSystem` is mainly written to keep track of mouse-events for entities; you can check whether your `Entity`
232-
has been hovered, clicked, dragged, etc. In order to use it, we therefore need an `Entity` which uses the `MouseSystem`.
233-
This one needs to hold a `MouseComponent`, at which the results/data will be saved.
232+
has been hovered, clicked, dragged, etc. In order to use it, we therefore need an `Entity` which uses the `MouseSystem`.
233+
This one needs to hold a `MouseComponent`, at which the results/data will be saved.
234234

235235
We first will update our `CityBuildingSystem` to contain the new `Entity`:
236236

@@ -252,10 +252,10 @@ this `CityBuildingSystem`:
252252
// New is the initialisation of the System
253253
func (cb *CityBuildingSystem) New(w *ecs.World) {
254254
fmt.Println("CityBuildingSystem was added to the Scene")
255-
255+
256256
cb.mouseTracker.BasicEntity = ecs.NewBasic()
257257
cb.mouseTracker.MouseComponent = common.MouseComponent{Track: true}
258-
258+
259259
for _, system := range w.Systems() {
260260
switch sys := system.(type) {
261261
case *common.MouseSystem:
@@ -269,12 +269,12 @@ Note that we added the `Track: true` variable to the `MouseComponent`. This allo
269269
mouse, regardless of where it is. If we were to leave it at `false` (the default), it would only contain anything
270270
useful if the mouse was hovering the `Entity`. We are also giving two parameters `nil` within the `Add` function
271271
of the `MouseSystem`. That is because (as described in the documentation of that `Add` function), one can also provide
272-
a `SpaceComponent` and `RenderComponent`, if one wants to know specifics about that particular entity (like
272+
a `SpaceComponent` and `RenderComponent`, if one wants to know specifics about that particular entity (like
273273
hover-events). This is not our intention at the moment, we just want to know about the position of the cursor. Therefore
274-
we can safely pass `nil` for those two parameters.
274+
we can safely pass `nil` for those two parameters.
275275

276276
##### Getting information from the MouseSystem
277-
The `MouseSystem` updates the `mouseTracker.MouseComponent` every frame. Everything we need to know, is in there.
277+
The `MouseSystem` updates the `mouseTracker.MouseComponent` every frame. Everything we need to know, is in there.
278278

279279
{% highlight go %}
280280
// Update is ran every frame, with `dt` being the time
@@ -312,7 +312,7 @@ func (cb *CityBuildingSystem) Update(dt float32) {
312312
{% endhighlight %}
313313

314314
But we still don't have a `world` reference within our `Update` function. We are given one inside our `New` function,
315-
so let's save that reference.
315+
so let's save that reference.
316316

317317
{% highlight go %}
318318
type CityBuildingSystem struct {
@@ -330,7 +330,7 @@ func (cb *CityBuildingSystem) New(w *ecs.World) {
330330
}
331331
{% endhighlight %}
332332

333-
Now, we can reference it by saying `cb.world`, within our `Update` function.
333+
Now, we can reference it by saying `cb.world`, within our `Update` function.
334334

335335
Now, if we were to run this whole project, and place a few cities using the **F1**-key, we would get something like
336336
this:

0 commit comments

Comments
 (0)