Skip to content

Commit

Permalink
Added 1.6 lesson - waiting for revision
Browse files Browse the repository at this point in the history
  • Loading branch information
desk467 committed Oct 21, 2015
1 parent 4d39e60 commit 0c72b33
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions book/html.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ include::world1_the-prototype/02_the-tools.adoc[]
include::world1_the-prototype/03_starting-small.adoc[]
include::world1_the-prototype/04_creating-motion.adoc[]
include::world1_the-prototype/05_better-steering.adoc[]
include::world1_the-prototype/06_adding-some-action.adoc[]

include::world2.adoc[]

Expand Down
2 changes: 2 additions & 0 deletions book/pdf.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ include::world1_the-prototype/04_creating-motion.adoc[]
<<<
include::world1_the-prototype/05_better-steering.adoc[]
<<<
include::world1_the-prototype/06_adding-some-action.adoc[]
<<<

include::world2.adoc[]
<<<
Expand Down
33 changes: 33 additions & 0 deletions book/world1_the-prototype/06_adding-some-action.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[[world1-6]]
=== Adding some action
Our prototype is getting better. Cool, isn't it?
Now we have the most aerodynamic ship ever and we can already go flying around. But what if something comes up and tries to attack us? The outer space is a very mysterious place. We don't know what to expect.
So, it could be interesting if we take with us a weapon to defend ourselves right?
Let's jump right in.

What we basically want is, when the player presses an action button, a group of bullets will be launched in the direction the ship is facing and each bullet will have a constant speed.
We will make something called `object pool`. So, when the player presses the action button, a new element will be inserted in this object pool. The algorithm of creation and manipulation of object pools is better explained in <<world3-1, World 3-1>>. I'd recommend you to give it a look.

code_example::world1/06_adding-some-action/creating-bullets[]
livecode::world1/06_adding-some-action/creating-bullets[]

This comment has been minimized.

Copy link
@s-ol

s-ol Oct 21, 2015

Member

you need to watch the order in which you commit things, submodule updates are included in other commits; right now this commit still uses the love2d-book-code commit that does not contain the code sample (git status should show modified: book/code (new commits)).

This comment has been minimized.

Copy link
@desk467

desk467 Oct 21, 2015

Author Contributor

git status shows me that everything are ok =o

This comment has been minimized.

Copy link
@s-ol

s-ol Oct 21, 2015

Member

did you clone love2d-book-code seperately? You shouldn't (probably), just cd into book/code and work in there

This comment has been minimized.

Copy link
@desk467

desk467 Oct 21, 2015

Author Contributor

my code directory does not have anything. i feel i've done a lot of things wrong :(
I think it's time to write a wiki explaining about helping with this project.

This comment has been minimized.

Copy link
@s-ol

s-ol Oct 21, 2015

Member

git submodule init


As you can see in this example, we created a table of `bullets`. This table stores every bullet that the player shoots.
So, as soon as the player presses `SPACE` key (in LÖVE, we use a blank space to do that), the function `shoot` is called. This function inserts a new bullet in the bullets table.
Every new bullet is created at the player's ship position as well as being launched in the direction the player's ship is facing. All bullets in `bullets` table will have a constant speed. Nothing much fearsome, but already it works very well.
Now that we've created our bullets, we need to create an algorithm for destroying them. Like Asteroids, our bullets must be destroyed in two situations:
1. When they go outside from the screen.
2. When they reach some asteroid.
For now we will focus only on the first situation, since we haven't asteroids to worry (yet).

code_example::world1/06_adding-some-action/destroying-bullets[]

So, we made some changes in our code:

1. We created a `bullets.trash` that is nothing but a table to place bullets that need to be destroyed.
2. Then, we created a `destroy_bullets` function that will verify which bullets are in the trash and delete them from our `bullets` table. After this mapping, we clean our trash.
We use a auxiliary table because we want to destroy bullets as they were created, to avoid future problems.
3. In the wiki:love.update[] function, as the bullets moved, we check if any of them came out of the horizontal and vertical limits of the game screen. If so, we put this bullet on our trash table. And then, at the very end of this function, we call our function `destroy_bullets` to remove all unnecessary bullets from memory

livecode::world1/06_adding-some-action/destroying-bullets[]

Note that, apparently, the game looks the same. But now we have a little less greedy version in relation to computer memory.

1 comment on commit 0c72b33

@s-ol
Copy link
Member

@s-ol s-ol commented on 0c72b33 Oct 21, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you are "waiting for revision" you should probably put it on a other branch and file a pull request. We could make that common procedure for adding a new level so as to keep the master branch in a stable, readable state from now on.

Please sign in to comment.