Added Collision with platforms. Added Gravity. And Changed some of the controls.

This commit is contained in:
Marco Salazar 2021-09-03 21:03:05 -06:00
parent 383b302c71
commit d1144e9ff2
4 changed files with 96 additions and 27 deletions

View File

@ -34,8 +34,26 @@ class MovingObject extends Component {
sprite.position = sprite.position - Vector2(velocity * dt, 0); sprite.position = sprite.position - Vector2(velocity * dt, 0);
} }
@override String intersect(Rect other) {
void remove() { final collision = sprite.toRect().intersect(other);
super.remove(); if (!collision.isEmpty) {
// print(collision);
double ydistance = other.top - sprite.toRect().top;
double xdistance = other.left - sprite.toRect().left;
if (ydistance.abs() > xdistance.abs()) {
if (ydistance > 0) {
return "bottom";
} else {
return "top";
}
} else {
if (xdistance > 0) {
return "right";
} else {
return "left";
}
}
}
return "none";
} }
} }

View File

@ -44,7 +44,6 @@ class PlatformHolder {
Platform platform = Platform(gameRef); Platform platform = Platform(gameRef);
platform.setPosition(xCordinate, gameRef.blockSize * level); platform.setPosition(xCordinate, gameRef.blockSize * level);
platforms[level].add(platform); platforms[level].add(platform);
print(platforms[0].length);
return false; return false;
} }
} }
@ -78,7 +77,7 @@ class PlatformHolder {
platformLevel.length > 3 && platformLevel.length > 3 &&
random.nextInt(100) > 65 && random.nextInt(100) > 65 &&
removed > 0) { removed > 0) {
int secondToLast = platformLevel.length - 3; int secondToLast = platformLevel.length - 4;
double secondToLastPosition = double secondToLastPosition =
platformLevel.elementAt(secondToLast).sprite.x; platformLevel.elementAt(secondToLast).sprite.x;
if (secondToLastPosition > gameRef.size.x) { if (secondToLastPosition > gameRef.size.x) {

View File

@ -1,6 +1,7 @@
import 'package:firo_runner/main.dart'; import 'package:firo_runner/main.dart';
import 'package:flame/effects.dart'; import 'package:flame/effects.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'Platform.dart';
import 'package:flame/components.dart'; import 'package:flame/components.dart';
@ -17,13 +18,13 @@ enum RunnerState {
class Runner extends Component with HasGameRef<MyGame> { class Runner extends Component with HasGameRef<MyGame> {
late SpriteAnimationGroupComponent sprite; late SpriteAnimationGroupComponent sprite;
String runnerState = "run";
void setPosition(Vector2 position) { void setPosition(Vector2 position) {
sprite.position = position; sprite.position = position;
} }
void setSize(Vector2 size, double ySize) { void setSize(Vector2 size, double ySize) {
// runnerSize = size;
sprite.size = size; sprite.size = size;
} }
@ -37,15 +38,16 @@ class Runner extends Component with HasGameRef<MyGame> {
getSprite().render(c, position: sprite.position, size: sprite.size); getSprite().render(c, position: sprite.position, size: sprite.size);
} }
int level = 7; int level = 1;
void updateLevel() { void updateLevel() {
level = (sprite.position.y / gameRef.blockSize).round(); level = (sprite.position.y / gameRef.blockSize).round();
} }
String runnerState = "run"; String previousState = "run";
void event(String event) { void event(String event) {
previousState = runnerState;
print(event); print(event);
switch (event) { switch (event) {
case "jump": case "jump":
@ -60,17 +62,20 @@ class Runner extends Component with HasGameRef<MyGame> {
curve: Curves.bounceIn, curve: Curves.bounceIn,
onComplete: () { onComplete: () {
updateLevel(); updateLevel();
runnerState = "run"; this.event("float");
}, },
)); ));
break; break;
case "doublejump": case "doublejump":
if (level - 1 < 0) {
break;
}
runnerState = event; runnerState = event;
sprite.current = RunnerState.jump; sprite.current = RunnerState.jump;
sprite.addEffect(MoveEffect( sprite.addEffect(MoveEffect(
path: [ path: [
sprite.position, sprite.position,
Vector2(sprite.x, (level - 3) * gameRef.blockSize), Vector2(sprite.x, (level - 2) * gameRef.blockSize),
], ],
speed: 50, speed: 50,
curve: Curves.ease, curve: Curves.ease,
@ -83,15 +88,21 @@ class Runner extends Component with HasGameRef<MyGame> {
case "fall": case "fall":
runnerState = event; runnerState = event;
sprite.current = RunnerState.fall; sprite.current = RunnerState.fall;
// TODO calculate distance to next platform.
sprite.addEffect(MoveEffect( sprite.addEffect(MoveEffect(
path: [ path: [
// sprite.position,
Vector2(sprite.x, (level + 1) * gameRef.blockSize), Vector2(sprite.x, (level + 1) * gameRef.blockSize),
], ],
speed: 50, speed: 100,
curve: Curves.ease, curve: Curves.ease,
onComplete: updateLevel, onComplete: () {
if (runnerState == "fall") {
updateLevel();
sprite.position = Vector2(sprite.x, level * gameRef.blockSize);
this.event("run");
} else {
this.event("float");
}
},
)); ));
break; break;
case "kick": case "kick":
@ -106,10 +117,7 @@ class Runner extends Component with HasGameRef<MyGame> {
runnerState = event; runnerState = event;
sprite.current = RunnerState.float; sprite.current = RunnerState.float;
sprite.addEffect(MoveEffect( sprite.addEffect(MoveEffect(
path: [ path: [sprite.position],
sprite.position,
Vector2(sprite.x, (level - 1) * gameRef.blockSize),
],
speed: 50, speed: 50,
curve: Curves.ease, curve: Curves.ease,
onComplete: () { onComplete: () {
@ -137,13 +145,12 @@ class Runner extends Component with HasGameRef<MyGame> {
} }
void control(String input) { void control(String input) {
print(runnerState + " " + input); print(input);
print(sprite.position);
switch (input) { switch (input) {
case "up": case "up":
if (runnerState == "run") { if (runnerState == "run") {
event("jump"); event("jump");
} else if (runnerState == "jump") { } else if (runnerState == "float" && previousState == "jump") {
event("doublejump"); event("doublejump");
} else if (runnerState == "duck") { } else if (runnerState == "duck") {
event("run"); event("run");
@ -152,6 +159,8 @@ class Runner extends Component with HasGameRef<MyGame> {
case "down": case "down":
if (runnerState == "run") { if (runnerState == "run") {
event("duck"); event("duck");
} else if (runnerState == "float" && onTopOfPlatform()) {
event("run");
} else if (runnerState == "float") { } else if (runnerState == "float") {
event("fall"); event("fall");
} }
@ -162,7 +171,8 @@ class Runner extends Component with HasGameRef<MyGame> {
} }
break; break;
case "center": case "center":
if (runnerState == "jump") { if (runnerState == "fall") {
updateLevel();
event("float"); event("float");
} }
break; break;
@ -181,9 +191,55 @@ class Runner extends Component with HasGameRef<MyGame> {
sprite.current = RunnerState.run; sprite.current = RunnerState.run;
} }
intersecting();
sprite.update(dt); sprite.update(dt);
} }
bool onTopOfPlatform() {
Rect runnerRect = sprite.toRect();
bool onTopOfPlatform = false;
for (List<Platform> platformLevel in gameRef.platformHolder.platforms) {
for (Platform p in platformLevel) {
String side = p.intersect(runnerRect);
if (side == "none") {
Rect belowRunner = Rect.fromLTRB(runnerRect.left, runnerRect.top,
runnerRect.right, runnerRect.bottom + 1);
if (p.intersect(belowRunner) != "none") {
onTopOfPlatform = true;
}
}
}
}
return onTopOfPlatform;
}
void intersecting() {
Rect runnerRect = sprite.toRect();
bool onTopOfPlatform = false;
for (List<Platform> platformLevel in gameRef.platformHolder.platforms) {
for (Platform p in platformLevel) {
String side = p.intersect(runnerRect);
if (side == "none") {
Rect belowRunner = Rect.fromLTRB(runnerRect.left, runnerRect.top,
runnerRect.right, runnerRect.bottom + 1);
if (p.intersect(belowRunner) != "none") {
onTopOfPlatform = true;
}
} else if (side == "bottom") {
// The runner has hit his head on the ceiling and should die.
event("die");
}
}
}
if (!onTopOfPlatform &&
(runnerState == "run" ||
runnerState == "kick" ||
runnerState == "duck")) {
event("fall");
}
}
Future load(loadSpriteAnimation) async { Future load(loadSpriteAnimation) async {
SpriteAnimation running = await loadSpriteAnimation( SpriteAnimation running = await loadSpriteAnimation(
'run-frames.png', 'run-frames.png',

View File

@ -59,11 +59,9 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
debugMode = true; debugMode = true;
print("load");
FlameAudio.bgm.initialize(); FlameAudio.bgm.initialize();
background = await Flame.images.load('bg.png'); background = await Flame.images.load('bg.png');
background1 = Sprite(background); background1 = Sprite(background);
print(background.height.toString() + " " + background.width.toString());
background2 = Sprite(background); background2 = Sprite(background);
platform1 = await Flame.images.load('platform1.png'); platform1 = await Flame.images.load('platform1.png');
platform2 = await Flame.images.load('platform2.png'); platform2 = await Flame.images.load('platform2.png');
@ -81,7 +79,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
runner = Runner(); runner = Runner();
await runner.load(loadSpriteAnimation); await runner.load(loadSpriteAnimation);
runner.setSize(runnerSize, blockSize); runner.setSize(runnerSize, blockSize);
runnerPosition = Vector2(blockSize, blockSize * 7); runnerPosition = Vector2(blockSize, blockSize * 1);
runner.setPosition(runnerPosition); runner.setPosition(runnerPosition);
add(runner); add(runner);
@ -126,14 +124,12 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
super.update(dt); super.update(dt);
gameState.update(dt); gameState.update(dt);
platformHolder.update(dt); platformHolder.update(dt);
// print(gameState.distance);
} }
@override @override
void onResize(Vector2 size) { void onResize(Vector2 size) {
super.onResize(size); super.onResize(size);
blockSize = size.y / 9; blockSize = size.y / 9;
print(blockSize);
runnerSize = Vector2( runnerSize = Vector2(
size.y / 9, size.y / 9,
size.y / 9, size.y / 9,