Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
feat: spawn player on screen
Browse files Browse the repository at this point in the history
  • Loading branch information
marcossevilla committed Oct 10, 2023
1 parent e4329e9 commit 6def669
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 88 deletions.
1 change: 0 additions & 1 deletion lib/game/behaviors/behaviors.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export 'flying_behavior.dart';
export 'gravity_behavior.dart';
export 'jumping_behavior.dart';
export 'obstacle_scrolling_behavior.dart';
Expand Down
43 changes: 0 additions & 43 deletions lib/game/behaviors/flying_behavior.dart

This file was deleted.

2 changes: 0 additions & 2 deletions lib/game/behaviors/gravity_behavior.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ class GravityBehavior extends Behavior<Player> with HasGameRef<DashRunGame> {
parent
..y = boundary
..yVelocity = 0;

parent.findBehavior<FlyingBehavior>().enabled = false;
}
}
}
30 changes: 0 additions & 30 deletions lib/game/behaviors/player_keyboard_controller_behavior.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import 'package:flame_behaviors/flame_behaviors.dart';
import 'package:flutter/services.dart';

class PlayerKeyboardControllerBehavior extends Behavior<Player> {
static const _flyHoldLimit = 0.5;

double _flyHoldTimer = 0;

@override
FutureOr<void> onLoad() async {
await super.onLoad();
Expand All @@ -18,41 +14,15 @@ class PlayerKeyboardControllerBehavior extends Behavior<Player> {
KeyboardListenerComponent(
keyDown: {
LogicalKeyboardKey.space: (_) {
final flying = parent.findBehavior<FlyingBehavior>();
if (flying.enabled) {
flying.climb();
} else if (_flyHoldTimer <= 0) {
_flyHoldTimer = _flyHoldLimit;
}

return false;
},
},
keyUp: {
LogicalKeyboardKey.space: (_) {
final flying = parent.findBehavior<FlyingBehavior>();
if (flying.enabled) {
flying.glide();
} else {
_flyHoldTimer = 0;
parent.findBehavior<JumpingBehavior>().jump();
}
return false;
},
},
),
);
}

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

if (_flyHoldTimer > 0) {
_flyHoldTimer -= dt;
if (_flyHoldTimer <= 0) {
parent.findBehavior<FlyingBehavior>().enabled = true;
}
}
}
}
18 changes: 6 additions & 12 deletions lib/game/components/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import 'dart:async';
import 'package:dash_run/game/game.dart';
import 'package:dash_run/gen/assets.gen.dart';
import 'package:flame/cache.dart';

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame_behaviors/flame_behaviors.dart';
import 'package:flame_tiled/flame_tiled.dart';

class Player extends PositionedEntity with HasGameRef<DashRunGame> {
Player()
Expand All @@ -18,18 +18,12 @@ class Player extends PositionedEntity with HasGameRef<DashRunGame> {
parentSize: _size,
),
),
GravityBehavior(),
PlayerKeyboardControllerBehavior(),
JumpingBehavior(),
FlyingBehavior(),
PlayerCollidingBehavior(),
],
);

static final _size = Vector2.all(100);
static final _size = Vector2.all(32);
static final _images = Images(prefix: '');

late final SpriteComponent flyingSprite;
late final SpriteAnimationComponent runningAnimation;

double yVelocity = 0;
Expand Down Expand Up @@ -66,10 +60,10 @@ class Player extends PositionedEntity with HasGameRef<DashRunGame> {

add(runningAnimation);

position = Vector2(
200,
gameRef.resolution.y - DashRunGame.floorSize,
);
final playerSpawn = gameRef.leapMap.getTileLayer<ObjectGroup>('spawn');
for (final spawn in playerSpawn.objects) {
position = Vector2(spawn.x, spawn.y);
}
}

void jump() => findBehavior<JumpingBehavior>().jump();
Expand Down
3 changes: 3 additions & 0 deletions lib/game/dash_run_game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class DashRunGame extends FixedResolutionGame
items = Items();
enemies = Enemies();
await addAll([items, enemies]);

player = Player();
add(player);
}

void gameOver() {
Expand Down

0 comments on commit 6def669

Please sign in to comment.