Skip to content

Commit

Permalink
feat: 🎸 create custom_effects & bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
panicdragon committed Apr 19, 2023
1 parent 1f03e6f commit 76153c4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 55 deletions.
46 changes: 12 additions & 34 deletions lib/components/crate.dart
Original file line number Diff line number Diff line change
@@ -1,54 +1,34 @@
import 'package:flame/components.dart';
import 'package:flame/sprite.dart';
import 'package:flame/effects.dart';
import 'package:flutter/material.dart';

import '../utility/config.dart';
import '../utility/custom_effects.dart';

class Crate extends SpriteAnimationComponent with HasGameRef {
int _moveCount = 0;
// int _moveCount = 0;
late Vector2 coordinate;
bool isGoal = false;

late final SpriteAnimation _noAnimation;
late final OpacityEffect _goalEffect = OpacityEffect.fadeOut(
EffectController(
duration: 0.6,
reverseDuration: 0.6,
infinite: true,
),
);
// late final ColorEffect _goalEffect = ColorEffect(
// Colors.blue,
// const Offset(
// 0.2,
// 0.8,
// ),
// EffectController(
// duration: 0.8,
// reverseDuration: 0.8,
// infinite: true,
// ),
// );
late final OpacityEffect _goalEffect;

Crate()
: super(
size: Vector2.all(64.0),
size: Vector2.all(oneBlockSize),
);

@override
Future<void> onLoad() async {
super.onLoad();
await _loadAnimations().then((_) => {animation = _noAnimation});
}

@override
void update(double delta) {
super.update(delta);
_goalEffect = customOpacityEffect;
}

Future<void> _loadAnimations() async {
final spriteSheet = SpriteSheet(
image: await gameRef.images.load('crate.png'),
srcSize: Vector2.all(64.0),
srcSize: Vector2.all(oneBlockSize),
);

_noAnimation =
Expand All @@ -60,7 +40,7 @@ class Crate extends SpriteAnimationComponent with HasGameRef {
}

move(Vector2 vec) {
moveFunc((vec - coordinate) * 64.0);
moveFunc((vec - coordinate) * oneBlockSize);
setPosition(vec);
}

Expand All @@ -73,11 +53,9 @@ class Crate extends SpriteAnimationComponent with HasGameRef {

if (isGoal && !_goalEffect.isMounted) {
add(_goalEffect);
} else if(!isGoal) {
if (_goalEffect.isMounted) {
_goalEffect.apply(0);
_goalEffect.removeFromParent();
}
} else if(!isGoal && _goalEffect.isMounted) {
_goalEffect.apply(0);
_goalEffect.removeFromParent();
}
}
}
11 changes: 6 additions & 5 deletions lib/components/player.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flame/components.dart';
import 'package:flame/sprite.dart';

import '../utility/config.dart';
import '../utility/direction.dart';

class Player extends SpriteAnimationComponent with HasGameRef {
Expand All @@ -18,7 +19,7 @@ class Player extends SpriteAnimationComponent with HasGameRef {

Player()
: super(
size: Vector2.all(64.0),
size: Vector2.all(oneBlockSize),
);

@override
Expand All @@ -34,9 +35,9 @@ class Player extends SpriteAnimationComponent with HasGameRef {
int get moveCount => _moveCount;

@override
void update(double delta) {
super.update(delta);
movePlayer(delta);
void update(double dt) {
super.update(dt);
movePlayer(dt);
}

void movePlayer(double delta) {
Expand Down Expand Up @@ -88,7 +89,7 @@ class Player extends SpriteAnimationComponent with HasGameRef {
}

void moveFunc(Vector2 vac) {
_moveCount -= _moveCoordinate as int;
_moveCount -= _moveCoordinate.toInt();
position.add(vac);
}
}
28 changes: 15 additions & 13 deletions lib/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import 'components/player.dart';
import 'components/crate.dart';

import 'src/push_game.dart';
import 'utility/config.dart';
import 'utility/direction.dart';

class MainGame extends FlameGame with KeyboardEvents, HasGameRef {
PushGame pushGame = PushGame();
final double _blockSize = 64;
late Player _player;
final List<Crate> _crateList = [];
final List<SpriteComponent> _bgComponentList = [];
Expand Down Expand Up @@ -41,38 +41,40 @@ class MainGame extends FlameGame with KeyboardEvents, HasGameRef {
for (var x = 0; x < row.length; x++) {
final char = row[x];

if (_spriteMap.containsKey(char)) renderBackGround(_spriteMap[char], x, y);
if (char == 'p') initPlayer(x, y);
if (char == 'o') initCrate(x, y);
if (_spriteMap.containsKey(char)) renderBackGround(_spriteMap[char], x.toDouble(), y.toDouble());
if (char == 'p') initPlayer(x.toDouble(), y.toDouble());
if (char == 'o') initCrate(x.toDouble(), y.toDouble());
}
}

add(_player);
for(var crate in _crateList) {
add(crate);
}
// camera.followComponent(_player);
if (pushGame.state.width > 20) {
camera.followComponent(_player);
}
}

void renderBackGround(sprite, x, y) {
void renderBackGround(sprite, double x, double y) {
final component = SpriteComponent(
size: Vector2.all(_blockSize),
size: Vector2.all(oneBlockSize),
sprite: sprite,
position: Vector2(x * _blockSize, y * _blockSize),
position: Vector2(x * oneBlockSize, y * oneBlockSize),
);
_bgComponentList.add(component);
add(component);
}

void initPlayer(x, y) {
void initPlayer(double x, double y) {
_player = Player();
_player.position = Vector2(x * _blockSize, y * _blockSize);
_player.position = Vector2(x * oneBlockSize, y * oneBlockSize);
}

void initCrate(x, y) {
void initCrate(double x, double y) {
final crate = Crate();
crate.setPosition(Vector2(x, y));
crate.position = Vector2(x * _blockSize, y * _blockSize);
crate.position = Vector2(x * oneBlockSize, y * oneBlockSize);
_crateList.add(crate);
}

Expand Down Expand Up @@ -133,7 +135,7 @@ class MainGame extends FlameGame with KeyboardEvents, HasGameRef {
void playerMove(bool isKeyDown, Direction keyDirection) {
if (isKeyDown && keyDirection != Direction.none) {
_player.direction = keyDirection;
_player.moveCount = _blockSize as int;
_player.moveCount = oneBlockSize.toInt();
} else if (_player.direction == keyDirection) {
_player.direction = Direction.none;
}
Expand Down
1 change: 1 addition & 0 deletions lib/utility/config.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const double oneBlockSize = 64.0;
23 changes: 23 additions & 0 deletions lib/utility/custom_effects.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:flame/effects.dart';
import 'package:flutter/material.dart';

OpacityEffect customOpacityEffect = OpacityEffect.fadeOut(
EffectController(
duration: 0.6,
reverseDuration: 0.6,
infinite: true,
),
);

ColorEffect customColorEffect = ColorEffect(
Colors.blue,
const Offset(
0.2,
0.8,
),
EffectController(
duration: 0.8,
reverseDuration: 0.8,
infinite: true,
),
);
3 changes: 0 additions & 3 deletions lib/utility/sprite_map.dart

This file was deleted.

0 comments on commit 76153c4

Please sign in to comment.