Added Collision with platforms. Added Gravity. And Changed some of the controls.
This commit is contained in:
parent
383b302c71
commit
d1144e9ff2
@ -34,8 +34,26 @@ class MovingObject extends Component {
|
||||
sprite.position = sprite.position - Vector2(velocity * dt, 0);
|
||||
}
|
||||
|
||||
@override
|
||||
void remove() {
|
||||
super.remove();
|
||||
String intersect(Rect other) {
|
||||
final collision = sprite.toRect().intersect(other);
|
||||
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";
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,6 @@ class PlatformHolder {
|
||||
Platform platform = Platform(gameRef);
|
||||
platform.setPosition(xCordinate, gameRef.blockSize * level);
|
||||
platforms[level].add(platform);
|
||||
print(platforms[0].length);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -78,7 +77,7 @@ class PlatformHolder {
|
||||
platformLevel.length > 3 &&
|
||||
random.nextInt(100) > 65 &&
|
||||
removed > 0) {
|
||||
int secondToLast = platformLevel.length - 3;
|
||||
int secondToLast = platformLevel.length - 4;
|
||||
double secondToLastPosition =
|
||||
platformLevel.elementAt(secondToLast).sprite.x;
|
||||
if (secondToLastPosition > gameRef.size.x) {
|
||||
|
@ -1,6 +1,7 @@
|
||||
import 'package:firo_runner/main.dart';
|
||||
import 'package:flame/effects.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'Platform.dart';
|
||||
|
||||
import 'package:flame/components.dart';
|
||||
|
||||
@ -17,13 +18,13 @@ enum RunnerState {
|
||||
|
||||
class Runner extends Component with HasGameRef<MyGame> {
|
||||
late SpriteAnimationGroupComponent sprite;
|
||||
String runnerState = "run";
|
||||
|
||||
void setPosition(Vector2 position) {
|
||||
sprite.position = position;
|
||||
}
|
||||
|
||||
void setSize(Vector2 size, double ySize) {
|
||||
// runnerSize = size;
|
||||
sprite.size = size;
|
||||
}
|
||||
|
||||
@ -37,15 +38,16 @@ class Runner extends Component with HasGameRef<MyGame> {
|
||||
getSprite().render(c, position: sprite.position, size: sprite.size);
|
||||
}
|
||||
|
||||
int level = 7;
|
||||
int level = 1;
|
||||
|
||||
void updateLevel() {
|
||||
level = (sprite.position.y / gameRef.blockSize).round();
|
||||
}
|
||||
|
||||
String runnerState = "run";
|
||||
String previousState = "run";
|
||||
|
||||
void event(String event) {
|
||||
previousState = runnerState;
|
||||
print(event);
|
||||
switch (event) {
|
||||
case "jump":
|
||||
@ -60,17 +62,20 @@ class Runner extends Component with HasGameRef<MyGame> {
|
||||
curve: Curves.bounceIn,
|
||||
onComplete: () {
|
||||
updateLevel();
|
||||
runnerState = "run";
|
||||
this.event("float");
|
||||
},
|
||||
));
|
||||
break;
|
||||
case "doublejump":
|
||||
if (level - 1 < 0) {
|
||||
break;
|
||||
}
|
||||
runnerState = event;
|
||||
sprite.current = RunnerState.jump;
|
||||
sprite.addEffect(MoveEffect(
|
||||
path: [
|
||||
sprite.position,
|
||||
Vector2(sprite.x, (level - 3) * gameRef.blockSize),
|
||||
Vector2(sprite.x, (level - 2) * gameRef.blockSize),
|
||||
],
|
||||
speed: 50,
|
||||
curve: Curves.ease,
|
||||
@ -83,15 +88,21 @@ class Runner extends Component with HasGameRef<MyGame> {
|
||||
case "fall":
|
||||
runnerState = event;
|
||||
sprite.current = RunnerState.fall;
|
||||
// TODO calculate distance to next platform.
|
||||
sprite.addEffect(MoveEffect(
|
||||
path: [
|
||||
// sprite.position,
|
||||
Vector2(sprite.x, (level + 1) * gameRef.blockSize),
|
||||
],
|
||||
speed: 50,
|
||||
speed: 100,
|
||||
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;
|
||||
case "kick":
|
||||
@ -106,10 +117,7 @@ class Runner extends Component with HasGameRef<MyGame> {
|
||||
runnerState = event;
|
||||
sprite.current = RunnerState.float;
|
||||
sprite.addEffect(MoveEffect(
|
||||
path: [
|
||||
sprite.position,
|
||||
Vector2(sprite.x, (level - 1) * gameRef.blockSize),
|
||||
],
|
||||
path: [sprite.position],
|
||||
speed: 50,
|
||||
curve: Curves.ease,
|
||||
onComplete: () {
|
||||
@ -137,13 +145,12 @@ class Runner extends Component with HasGameRef<MyGame> {
|
||||
}
|
||||
|
||||
void control(String input) {
|
||||
print(runnerState + " " + input);
|
||||
print(sprite.position);
|
||||
print(input);
|
||||
switch (input) {
|
||||
case "up":
|
||||
if (runnerState == "run") {
|
||||
event("jump");
|
||||
} else if (runnerState == "jump") {
|
||||
} else if (runnerState == "float" && previousState == "jump") {
|
||||
event("doublejump");
|
||||
} else if (runnerState == "duck") {
|
||||
event("run");
|
||||
@ -152,6 +159,8 @@ class Runner extends Component with HasGameRef<MyGame> {
|
||||
case "down":
|
||||
if (runnerState == "run") {
|
||||
event("duck");
|
||||
} else if (runnerState == "float" && onTopOfPlatform()) {
|
||||
event("run");
|
||||
} else if (runnerState == "float") {
|
||||
event("fall");
|
||||
}
|
||||
@ -162,7 +171,8 @@ class Runner extends Component with HasGameRef<MyGame> {
|
||||
}
|
||||
break;
|
||||
case "center":
|
||||
if (runnerState == "jump") {
|
||||
if (runnerState == "fall") {
|
||||
updateLevel();
|
||||
event("float");
|
||||
}
|
||||
break;
|
||||
@ -181,9 +191,55 @@ class Runner extends Component with HasGameRef<MyGame> {
|
||||
sprite.current = RunnerState.run;
|
||||
}
|
||||
|
||||
intersecting();
|
||||
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 {
|
||||
SpriteAnimation running = await loadSpriteAnimation(
|
||||
'run-frames.png',
|
||||
|
@ -59,11 +59,9 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
|
||||
@override
|
||||
Future<void> onLoad() async {
|
||||
debugMode = true;
|
||||
print("load");
|
||||
FlameAudio.bgm.initialize();
|
||||
background = await Flame.images.load('bg.png');
|
||||
background1 = Sprite(background);
|
||||
print(background.height.toString() + " " + background.width.toString());
|
||||
background2 = Sprite(background);
|
||||
platform1 = await Flame.images.load('platform1.png');
|
||||
platform2 = await Flame.images.load('platform2.png');
|
||||
@ -81,7 +79,7 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
|
||||
runner = Runner();
|
||||
await runner.load(loadSpriteAnimation);
|
||||
runner.setSize(runnerSize, blockSize);
|
||||
runnerPosition = Vector2(blockSize, blockSize * 7);
|
||||
runnerPosition = Vector2(blockSize, blockSize * 1);
|
||||
runner.setPosition(runnerPosition);
|
||||
add(runner);
|
||||
|
||||
@ -126,14 +124,12 @@ class MyGame extends BaseGame with PanDetector, TapDetector, KeyboardEvents {
|
||||
super.update(dt);
|
||||
gameState.update(dt);
|
||||
platformHolder.update(dt);
|
||||
// print(gameState.distance);
|
||||
}
|
||||
|
||||
@override
|
||||
void onResize(Vector2 size) {
|
||||
super.onResize(size);
|
||||
blockSize = size.y / 9;
|
||||
print(blockSize);
|
||||
runnerSize = Vector2(
|
||||
size.y / 9,
|
||||
size.y / 9,
|
||||
|
Loading…
Reference in New Issue
Block a user