Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Renan Araujo committed Nov 17, 2018
1 parent cc0277b commit 6e843c8
Show file tree
Hide file tree
Showing 20 changed files with 274 additions and 322 deletions.
31 changes: 11 additions & 20 deletions lib/game/Game.dart
Expand Up @@ -10,8 +10,7 @@ import 'package:trex/game/t_rex/t_rex.dart';

enum TRexGameStatus { playing, waiting, gameOver }

class TRexGame extends BaseGame{

class TRexGame extends BaseGame {
TRex tRex;
Horizon horizon;
GameOverPanel gameOverPanel;
Expand All @@ -20,20 +19,16 @@ class TRexGame extends BaseGame{
double currentSpeed = GameConfig.speed;
double timePlaying = 0.0;

TRexGame({
Image spriteImage
}) {
TRexGame({Image spriteImage}) {
tRex = new TRex(spriteImage);
horizon = new Horizon(spriteImage);
gameOverPanel = new GameOverPanel(spriteImage);

this..add(horizon)..add(tRex)..add(gameOverPanel);

}


void onTap() {
if(gameOver){
if (gameOver) {
restart();
return;
}
Expand All @@ -42,42 +37,41 @@ class TRexGame extends BaseGame{

@override
void update(double t) {

tRex.update(t);
horizon.updateWithSpeed(0.0, this.currentSpeed);

if(gameOver) return;
if (gameOver) return;

if(tRex.playingIntro && tRex.x >= TRexConfig.startXPos ) {
if (tRex.playingIntro && tRex.x >= TRexConfig.startXPos) {
startGame();
} else if (tRex.playingIntro) {
horizon.updateWithSpeed(0.0, this.currentSpeed);
}

if(this.playing){
if (this.playing) {
timePlaying += t;
horizon.updateWithSpeed(t, this.currentSpeed);

var obstacles = horizon.horizonLine.obstacleManager.components;
bool collision = obstacles.length > 0 && checkForCollision(obstacles.first, tRex);
if(!collision){
bool collision =
obstacles.length > 0 && checkForCollision(obstacles.first, tRex);
if (!collision) {
if (this.currentSpeed < GameConfig.maxSpeed) {
this.currentSpeed += GameConfig.acceleration;
}
} else {
doGameOver();
}
}

}

void startGame () {
void startGame() {
tRex.status = TRexStatus.running;
status = TRexGameStatus.playing;
tRex.hasPlayedIntro = true;
}

bool get playing => status == TRexGameStatus.playing;
bool get playing => status == TRexGameStatus.playing;
bool get gameOver => status == TRexGameStatus.gameOver;

void doGameOver() {
Expand All @@ -97,8 +91,5 @@ class TRexGame extends BaseGame{
currentSpeed = GameConfig.speed;
gameOverPanel.visible = false;
timePlaying = 0.0;

}
}


5 changes: 2 additions & 3 deletions lib/game/collision/collision_box.dart
@@ -1,5 +1,4 @@

class CollisionBox{
class CollisionBox {
final double x;
final double y;
final double width;
Expand All @@ -10,4 +9,4 @@ class CollisionBox{
this.width,
this.height,
});
}
}
31 changes: 16 additions & 15 deletions lib/game/collision/collision_utils.dart
Expand Up @@ -15,22 +15,24 @@ bool checkForCollision(Obstacle obstacle, TRex tRex) {
CollisionBox obstacleBox = CollisionBox(
x: obstacle.x + 1,
y: obstacle.y + 1,
width: obstacle.type.width * obstacle.internalSize -2,
width: obstacle.type.width * obstacle.internalSize - 2,
height: obstacle.type.height - 2,
);

if(boxCompare(tRexBox, obstacleBox)){

if (boxCompare(tRexBox, obstacleBox)) {
List<CollisionBox> collisionBoxes = obstacle.collisionBoxes;
List<CollisionBox> tRexCollisionBoxes = tRex.ducking ? TRexCollisionBoxes.ducking : TRexCollisionBoxes.running;
List<CollisionBox> tRexCollisionBoxes =
tRex.ducking ? TRexCollisionBoxes.ducking : TRexCollisionBoxes.running;

bool crashed = false;

collisionBoxes.forEach((obstacleCollisionBox) {
CollisionBox adjObstacleBox = createAdjustedCollisionBox(obstacleCollisionBox, obstacleBox);
CollisionBox adjObstacleBox =
createAdjustedCollisionBox(obstacleCollisionBox, obstacleBox);

tRexCollisionBoxes.forEach((tRexCollisionBox){
CollisionBox adjTRexBox = createAdjustedCollisionBox(tRexCollisionBox, tRexBox);
tRexCollisionBoxes.forEach((tRexCollisionBox) {
CollisionBox adjTRexBox =
createAdjustedCollisionBox(tRexCollisionBox, tRexBox);
crashed = crashed || boxCompare(adjTRexBox, adjObstacleBox);
});
});
Expand All @@ -40,7 +42,6 @@ bool checkForCollision(Obstacle obstacle, TRex tRex) {
}

bool boxCompare(CollisionBox tRexBox, CollisionBox obstacleBox) {

final double obstacleX = obstacleBox.x;
final double obstacleY = obstacleBox.y;

Expand All @@ -50,11 +51,11 @@ bool boxCompare(CollisionBox tRexBox, CollisionBox obstacleBox) {
tRexBox.height + tRexBox.y > obstacleY);
}

CollisionBox createAdjustedCollisionBox(CollisionBox box, CollisionBox adjustment) {
CollisionBox createAdjustedCollisionBox(
CollisionBox box, CollisionBox adjustment) {
return CollisionBox(
x: box.x + adjustment.x,
y: box.y + adjustment.y,
width: box.width,
height: box.height
);
}
x: box.x + adjustment.x,
y: box.y + adjustment.y,
width: box.width,
height: box.height);
}
13 changes: 6 additions & 7 deletions lib/game/custom/composed_component.dart
@@ -1,12 +1,12 @@

import 'package:flame/components/component.dart';
import 'package:ordered_set/comparing.dart';
import 'package:ordered_set/ordered_set.dart';
import 'package:flutter/painting.dart';
import 'package:flame/components/resizable.dart';

abstract class ComposedComponent implements Component {
OrderedSet<Component> components = new OrderedSet(Comparing.on((c) => c.priority()));
OrderedSet<Component> components =
new OrderedSet(Comparing.on((c) => c.priority()));

@override
render(Canvas canvas) {
Expand All @@ -30,7 +30,7 @@ abstract class ComposedComponent implements Component {
void add(Component c) {
this.components.add(c);

if(this is Resizable){
if (this is Resizable) {
// first time resize
Resizable thisResizable = this as Resizable;
if (thisResizable.size != null) {
Expand All @@ -39,17 +39,16 @@ abstract class ComposedComponent implements Component {
}
}

void updateComponents (Function itractionCB){
void updateComponents(Function itractionCB) {
components.forEach(itractionCB);
}

@override
void resize(Size size) {
if(this is Resizable){
if (this is Resizable) {
Resizable thisResizable = this as Resizable;
thisResizable.size = size;
components.forEach((c) => c.resize(size));
}
}

}
}
5 changes: 2 additions & 3 deletions lib/game/custom/util.dart
@@ -1,7 +1,6 @@


import 'dart:math';

Random rnd = new Random();

double getRandomNum(double min, double max) => (rnd.nextDouble() * (max - min + 1)).floor() + min;
double getRandomNum(double min, double max) =>
(rnd.nextDouble() * (max - min + 1)).floor() + min;
3 changes: 1 addition & 2 deletions lib/game/game_config.dart
@@ -1,4 +1,3 @@

class GameConfig {
static double acceleration = 0.001;
static double bgCloudSpeed = 0.2;
Expand All @@ -19,4 +18,4 @@ class GameConfig {
static double minJumpHeight = 35.0;
static double speed = 6.5;
static double speedDropCoefficient = 3.0;
}
}
6 changes: 2 additions & 4 deletions lib/game/game_over/config.dart
@@ -1,8 +1,6 @@


class GameOverConfig{
class GameOverConfig {
static double textWidth = 382.0;
static double textHeight = 22.0;
static double restartWidth = 72.0;
static double restartHeight = 64.0;
}
}
66 changes: 31 additions & 35 deletions lib/game/game_over/game_over.dart
Expand Up @@ -6,8 +6,8 @@ import 'package:flame/sprite.dart';
import 'package:trex/game/custom/composed_component.dart';
import 'package:trex/game/game_over/config.dart';


class GameOverPanel extends PositionComponent with Resizable, ComposedComponent {
class GameOverPanel extends PositionComponent
with Resizable, ComposedComponent {
bool visible = false;

GameOverText gameOverText;
Expand All @@ -22,55 +22,51 @@ class GameOverPanel extends PositionComponent with Resizable, ComposedComponent

@override
void render(Canvas canvas) {
if(visible){
if (visible) {
super.render(canvas);
}
}

}


class GameOverText extends SpriteComponent with Resizable{
GameOverText(Image spriteImage): super.fromSprite(
GameOverConfig.textWidth,
GameOverConfig.textHeight,
Sprite.fromImage(spriteImage,
x: 955.0,
y: 26.0,
width: GameOverConfig.textWidth,
height: GameOverConfig.textHeight,
)
);
class GameOverText extends SpriteComponent with Resizable {
GameOverText(Image spriteImage)
: super.fromSprite(
GameOverConfig.textWidth,
GameOverConfig.textHeight,
Sprite.fromImage(
spriteImage,
x: 955.0,
y: 26.0,
width: GameOverConfig.textWidth,
height: GameOverConfig.textHeight,
));

@override
resize(Size size) {


if(width > size.width * 0.8){
if (width > size.width * 0.8) {
width = size.width * 0.8;
}
y = size.height * .25;
x = (size.width / 2) - width /2;

x = (size.width / 2) - width / 2;
}
}

class GameOverRestart extends SpriteComponent with Resizable{
GameOverRestart(Image spriteImage): super.fromSprite(
GameOverConfig.restartWidth,
GameOverConfig.restartHeight,
Sprite.fromImage(spriteImage,
x: 2.0,
y: 2.0,
width: GameOverConfig.restartWidth,
height: GameOverConfig.restartHeight,
)
);

class GameOverRestart extends SpriteComponent with Resizable {
GameOverRestart(Image spriteImage)
: super.fromSprite(
GameOverConfig.restartWidth,
GameOverConfig.restartHeight,
Sprite.fromImage(
spriteImage,
x: 2.0,
y: 2.0,
width: GameOverConfig.restartWidth,
height: GameOverConfig.restartHeight,
));

@override
resize(Size size) {
y = size.height * .75;
x = (size.width / 2) - GameOverConfig.restartWidth /2;
x = (size.width / 2) - GameOverConfig.restartWidth / 2;
}
}
}

0 comments on commit 6e843c8

Please sign in to comment.