mirror of
https://github.com/johrpan/christmas_cats.git
synced 2025-10-28 19:27:25 +01:00
Add coins to game
This adds a new asset, a new component and some new game mechanics. Coins will appear every one to two minutes and can be collected by tapping them.
This commit is contained in:
parent
72aa6d7183
commit
03c94fe48c
5 changed files with 258 additions and 0 deletions
75
lib/components/coin.dart
Normal file
75
lib/components/coin.dart
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import 'dart:ui';
|
||||
|
||||
import 'package:flame/anchor.dart';
|
||||
import 'package:flame/components/component.dart';
|
||||
import 'package:flame/sprite.dart';
|
||||
import 'package:flame/time.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
import '../storage.dart';
|
||||
|
||||
class Coin extends PositionComponent {
|
||||
static const flightTime = 0.8;
|
||||
|
||||
final sprite = Sprite('coin.png');
|
||||
final Vector2 position;
|
||||
final Vector2 target;
|
||||
|
||||
Timer killTimer;
|
||||
bool shouldDestroy = false;
|
||||
|
||||
bool flying = false;
|
||||
double flight = 0.0;
|
||||
|
||||
Coin(this.position, this.target, void Function() onDestroy) {
|
||||
x = position.x;
|
||||
y = position.y;
|
||||
width = 24.0;
|
||||
height = 24.0;
|
||||
anchor = Anchor.center;
|
||||
|
||||
killTimer = Timer(2.5, callback: () {
|
||||
onDestroy();
|
||||
shouldDestroy = true;
|
||||
});
|
||||
|
||||
killTimer.start();
|
||||
}
|
||||
|
||||
void tap() {
|
||||
killTimer.stop();
|
||||
flying = true;
|
||||
storage.addCoins(1);
|
||||
}
|
||||
|
||||
@override
|
||||
void render(Canvas c) {
|
||||
prepareCanvas(c);
|
||||
sprite.render(c, width: width, height: height);
|
||||
}
|
||||
|
||||
@override
|
||||
void update(double t) {
|
||||
killTimer.update(t);
|
||||
|
||||
if (flying) {
|
||||
flight += t / flightTime;
|
||||
|
||||
if (flight > 1.0) {
|
||||
flight = 1.0;
|
||||
flying = false;
|
||||
shouldDestroy = true;
|
||||
}
|
||||
|
||||
x = lerpDouble(position.x, target.x, Curves.easeIn.transform(flight));
|
||||
y = lerpDouble(position.y, target.y, Curves.easeInBack.transform(flight));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool destroy() => shouldDestroy;
|
||||
|
||||
@override
|
||||
int priority() => 25000;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue