Skip to content

Instantly share code, notes, and snippets.

@BoHellgren
Last active April 21, 2020 14:37
Show Gist options
  • Save BoHellgren/75fe7f919b3305db482cefa7d4b72d1e to your computer and use it in GitHub Desktop.
Save BoHellgren/75fe7f919b3305db482cefa7d4b72d1e to your computer and use it in GitHub Desktop.
Gnat class
class Gnat extends SpriteComponent with Tapable {
Sprite sprite;
double xMax, yMax;
double xSpeed, ySpeed;
double spriteSize;
final spriteSpeed = 18.0;
final spriteMin = 30.0;
final spriteMax = 60.0;
bool wall = false;
bool remove = false;
Gnat(this.sprite) : super.fromSprite(16.0, 16.0, sprite);
@override
void resize(Size size) {
Random random = Random();
spriteSize = spriteMin + random.nextDouble() * (spriteMax - spriteMin);
width = spriteSize;
height = spriteSize;
xMax = size.width;
x = random.nextDouble() * (xMax - width);
xSpeed = 120.0 + random.nextDouble() * 120.0;
if (random.nextBool()) xSpeed = -xSpeed;
yMax = size.height;
y = random.nextDouble() * (yMax - height);
ySpeed = 60.0 + random.nextDouble() * 240.0;
if (random.nextBool()) ySpeed = -ySpeed;
}
@override
void update(double dt) {
x += xSpeed * dt;
if (((x >= xMax - width) && xSpeed > 0) || (x < 0.0)) {
xSpeed = -xSpeed;
wall = true;
}
y += ySpeed * dt;
if (((y > yMax - height) && (ySpeed > 0)) || ((y < 35.0 && (ySpeed < 0)))) {
ySpeed = -ySpeed;
wall = true;
}
spriteSize += spriteSpeed * dt;
if ((spriteSize > spriteMax) && wall) spriteSize = spriteMin;
width = spriteSize;
height = spriteSize;
wall = false;
renderFlipX = xSpeed > 0 ? false : true;
angle = atan(ySpeed / xSpeed);
}
@override
void onTapDown(TapDownDetails details) {
MyGame.kills++;
remove = true;
}
@override
bool destroy() {
return remove;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment