This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rect dinoRect = dino.getRect(screenSize, runDistance).deflate(15); | |
for (Cactus obstacle in obstacles) { | |
Rect obstacleRect = obstacle.getRect(screenSize, runDistance); | |
if (dinoRect.overlaps(obstacleRect.deflate(15))) { | |
_die(); | |
} | |
if (obstacleRect.right < 0) { | |
setState(() { | |
obstacles.remove(obstacle); | |
obstacles.add(Cactus( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void _die() { | |
setState(() { | |
worldController.stop(); | |
dino.die(); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void die() { | |
state = DinoState.dead; | |
frame = 6; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Size screenSize = MediaQuery.of(context).size; | |
Rect dinoRect = dino.getRect(screenSize, runDistance).deflate(15); | |
for (Cactus obstacle in obstacles) { | |
Rect obstacleRect = obstacle.getRect(screenSize, runDistance); | |
if (dinoRect.overlaps(obstacleRect.deflate(15))) { | |
_die(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void _update() { | |
double elapsedSeconds = | |
((worldController.lastElapsedDuration.inMilliseconds - | |
lastUpdateCall.inMilliseconds) / | |
1000); | |
runDistance = max(runDistance + runSpeed elapsedSeconds, 0); | |
runSpeed += RUN_SPEED_ACC_PPSPS elapsedSeconds; | |
dino.update(lastUpdateCall, worldController.lastElapsedDuration); | |
lastUpdateCall = worldController.lastElapsedDuration | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (GameObject object in [...obstacles, dino]) { | |
children.add( | |
AnimatedBuilder( | |
animation: worldController, | |
builder: (context, child) { | |
Rect objectRect = object.getRect(screenSize, runDistance); | |
return Positioned( | |
top: objectRect.top, | |
left: objectRect.left, | |
width: objectRect.width, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Cactus> obstacles = [ | |
Cactus(location: Offset(200, 0)), | |
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(location.dx - runDistance) * WORLD_TO_PIXEL_RATIO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cactus extends GameObject { | |
// This is the location of the cactus in "world" coordinates which is translated to "screen" coordinates | |
final Offset worldLocation; | |
final Sprite sprite; | |
Cactus({this.location}) : sprite = CACTI[Random().nextInt(CACTI.length)]; | |
@override | |
Rect getRect(Size screenSize, double runDistance) { | |
return Rect.fromLTWH( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void jump() { | |
if (state != DinoState.jumping) { | |
velY = 650; | |
} | |
} |