Skip to content

Commit

Permalink
Use a Linked List for Pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
RobLoach committed Apr 4, 2018
1 parent c53aba0 commit fbe3410
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
*.state
*.state*
*.chailove
21 changes: 12 additions & 9 deletions GameState.chai
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,31 @@ class GameState {
}

// Update all pipes.
var removePipe = -1
for (var i = 0; i < pipes.size(); ++i) {
var removePipe = 0
for (pipe : pipes) {
// Check collisions with any pipes.
if (pipes[i].collide(bird)) {
if (pipe.collide(bird)) {
assets["hit"].play()
states.set("menu")
return
}

// Update will return false if we are to remove it.
if (pipes[i].update(delta, bird.speed) == false) {
removePipe = i
if (pipe.update(delta, bird.speed) == false) {
removePipe = removePipe + 1
}

// Count the score.
if (bird.x > pipes[i].x + pipes[i].width / 2 && pipes[i].top && pipes[i].scored == false) {
if (bird.x > pipe.x + pipe.width / 2 && pipe.top && pipe.scored == false) {
addScore()
pipes[i].scored = true
pipe.scored = true
}
}

// See if we are removing an old pipe.
if (removePipe >= 0) {
pipes.erase_at(removePipe)
while (removePipe > 0) {
pipes.pop_front()
removePipe = removePipe - 1
}

// Add a new pipe if needed.
Expand Down Expand Up @@ -76,9 +77,11 @@ class GameState {

flapTimer += delta
}

def load() {
// Nothing.
}

def draw() {
// Nothing.
}
Expand Down
1 change: 1 addition & 0 deletions Pipe.chai
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Pipe {
"quadheight": this.sourcequad.height
]
}

def deserialize(obj) {
this.x = obj["x"]
this.y = obj["y"]
Expand Down
4 changes: 2 additions & 2 deletions main.chai
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
global bird
global assets
global pipes = []
global pipes = List()
global pipetimer = 0.0f
global pipetimerCountdown = 1.4f
global score = 0
Expand All @@ -21,7 +21,7 @@ global states
*/
def conf(t) {
t.identity = "floppybird"
t.version = "0.14.0" // Version of ChaiLove
t.version = "0.18.0" // Version of ChaiLove
t.window.title = "Floppy Bird";
t.window.width = 480;
t.window.height = 480;
Expand Down

0 comments on commit fbe3410

Please sign in to comment.