2021-09-03 23:29:18 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
|
|
import 'package:firo_runner/main.dart';
|
|
|
|
import 'package:flame/components.dart';
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Class meant to be extended by any object that will move left on the screen.
|
|
|
|
// Ensures a relatively constant moving velocity, and takes care of sprite
|
|
|
|
// animations and positioning.
|
2021-09-04 22:56:08 +00:00
|
|
|
class MovingObject {
|
2021-09-03 23:29:18 +00:00
|
|
|
late SpriteAnimationGroupComponent sprite;
|
|
|
|
MyGame gameRef;
|
|
|
|
|
|
|
|
MovingObject(this.gameRef);
|
|
|
|
|
|
|
|
void setPosition(double x, double y) {
|
|
|
|
sprite.position = Vector2(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSize(double x, double y) {
|
|
|
|
sprite.size = Vector2(x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sprite getSprite() {
|
|
|
|
return sprite.animation!.getSprite();
|
|
|
|
}
|
|
|
|
|
|
|
|
void update(double dt) {
|
|
|
|
double velocity = gameRef.gameState.getVelocity();
|
|
|
|
sprite.position = sprite.position - Vector2(velocity * dt, 0);
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Get the rightmost pixel position of this sprite.
|
2021-09-16 21:15:32 +00:00
|
|
|
double getRightEnd() {
|
|
|
|
return sprite.position.x + sprite.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remove() {
|
|
|
|
sprite.remove();
|
|
|
|
}
|
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// See where this object intersects another object if at all.
|
2021-09-04 03:03:05 +00:00
|
|
|
String intersect(Rect other) {
|
|
|
|
final collision = sprite.toRect().intersect(other);
|
|
|
|
if (!collision.isEmpty) {
|
2021-09-04 22:56:08 +00:00
|
|
|
double yDistance = other.top - sprite.toRect().top;
|
|
|
|
double xDistance = other.left - sprite.toRect().left;
|
|
|
|
if (yDistance.abs() > xDistance.abs()) {
|
|
|
|
if (yDistance > 0) {
|
2021-09-04 03:03:05 +00:00
|
|
|
return "bottom";
|
|
|
|
} else {
|
|
|
|
return "top";
|
|
|
|
}
|
|
|
|
} else {
|
2021-09-04 22:56:08 +00:00
|
|
|
if (xDistance > 0) {
|
2021-09-04 03:03:05 +00:00
|
|
|
return "right";
|
|
|
|
} else {
|
|
|
|
return "left";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "none";
|
2021-09-03 23:29:18 +00:00
|
|
|
}
|
2021-09-16 17:38:46 +00:00
|
|
|
|
2021-10-09 18:32:15 +00:00
|
|
|
// Resize the object for chaning screen sizes.
|
2021-09-16 17:38:46 +00:00
|
|
|
void resize(Vector2 newSize, double xRatio, double yRatio) {
|
|
|
|
sprite.x *= xRatio;
|
|
|
|
sprite.y *= yRatio;
|
|
|
|
sprite.width *= xRatio;
|
|
|
|
sprite.height *= yRatio;
|
|
|
|
}
|
2021-09-03 23:29:18 +00:00
|
|
|
}
|