Skip to content

Instantly share code, notes, and snippets.

@BoHellgren
Last active September 24, 2020 17:12
Show Gist options
  • Save BoHellgren/d104aeb196465a88deb51e824b6eeec9 to your computer and use it in GitHub Desktop.
Save BoHellgren/d104aeb196465a88deb51e824b6eeec9 to your computer and use it in GitHub Desktop.
Game final gameclasses.dart
import 'package:flame/position.dart';
import 'package:flutter/material.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'dart:math';
import 'package:flame/components/component.dart';
import 'package:flame/components/mixins/tapable.dart';
import 'package:flame/sprite.dart';
import 'package:flame/text_config.dart';
import 'i18n.dart';
class MyGame extends BaseGame {
static int kills;
static int target;
static final Paint paint = Paint();
static Rect progressIndicatorRect;
static double progressIndicatorLength;
Rect bgRect;
double secondsPassed = 0.0;
@override
bool debugMode() {
return false;
}
@override
void resize(Size size) {
bgRect = Rect.fromLTWH(0.0, 0.0, size.width, size.height);
super.resize(size);
}
void update(double dt) {
if ((secondsPassed < 10.0) && (kills < target)) secondsPassed += dt;
super.update(dt);
}
@override
void render(Canvas canvas) {
paint.color = Colors.white;
canvas.drawRect(bgRect, paint);
super.render(canvas);
progressIndicatorLength =
min(secondsPassed * size.width / 10.0, size.width);
progressIndicatorRect =
Rect.fromLTWH(0.0, 0.0, progressIndicatorLength, 15.0);
paint.color = Colors.red;
canvas.drawRect(progressIndicatorRect, paint);
TextConfig(fontSize: 16.0, color: Colors.black,)
..render(canvas, I18n.currentKills + ' ${MyGame.kills}', Position(5.0, size.height - 20.0));
}
}
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++;
Flame.audio.play('28845__junggle__btn035.wav');
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