Last active
July 4, 2022 17:00
-
-
Save immadisairaj/521768c71ab8ccb7410f99b9af502848 to your computer and use it in GitHub Desktop.
Flame Water Wave #FlameEffectsChallenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flame/components.dart'; | |
import 'package:flame/game.dart'; | |
import 'package:flame/effects.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp( | |
GameWidget(game: FlameWave()), | |
); | |
} | |
double edgeLength = 400.0; | |
double edgeWidth = 0.1; | |
class FlameWave extends FlameGame { | |
@override | |
Future<void> onLoad() async { | |
add(RectangleComponent( | |
size: size, | |
position: Vector2.all(0), | |
paint: Paint()..color = Colors.blue, | |
)); | |
const color = Colors.cyan; | |
final xCount = (size.x / edgeWidth).ceil(); | |
for (var x = 0; x < xCount; x++) { | |
add(WaterComponent( | |
position: Vector2(x * edgeWidth / 2, 0), | |
paint: Paint()..color = color, | |
size: Vector2(edgeWidth, edgeLength), | |
index: (x/3).ceil(), | |
)); | |
} | |
} | |
} | |
class WaterComponent extends PositionComponent { | |
final int index; | |
WaterComponent( | |
{required Paint paint, | |
required super.size, | |
required super.position, | |
required this.index}) | |
: super(children: [ | |
RectangleComponent( | |
position: position, | |
size: size, | |
paint: paint, | |
) | |
]); | |
_reduceScaleEffect() { | |
add(ScaleEffect.to( | |
Vector2(1, 0.9), | |
EffectController( | |
duration: 1, | |
infinite: true, | |
alternate: true, | |
curve: Curves.easeInOut), | |
onComplete: _addScaleEffect, | |
)); | |
} | |
_addScaleEffect({double delay = 0}) { | |
add(ScaleEffect.to( | |
Vector2(1, 1.1), | |
EffectController( | |
duration: 1, | |
infinite: true, | |
alternate: true, | |
curve: Curves.easeInOut, | |
startDelay: delay), | |
onComplete: _reduceScaleEffect, | |
)); | |
} | |
@override | |
Future<void> onLoad() async => _addScaleEffect(delay: index / 1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment