Skip to content

Instantly share code, notes, and snippets.

@castrors
Last active December 4, 2024 08:00
Show Gist options
  • Save castrors/d60bb7d665388b6735f183a295233177 to your computer and use it in GitHub Desktop.
Save castrors/d60bb7d665388b6735f183a295233177 to your computer and use it in GitHub Desktop.
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(GameWidget(game: MyGame()));
}
class Player extends PositionComponent
with HasGameReference<MyGame>, KeyboardHandler, CollisionCallbacks {
Player() : super(size: Vector2(50, 100), anchor: Anchor.center);
int horizontalDirection = 0;
int verticalDirection = 0;
final Vector2 velocity = Vector2.zero();
final double moveSpeed = 200;
bool hasSpacePressed = false;
bool get holdingCorn => corn != null;
Corn? corn;
Corn? collisionCorn;
@override
Future<void> onLoad() async {
await super.onLoad();
add(RectangleHitbox());
position = game.size / 2;
}
@override
void render(Canvas canvas) {
super.render(canvas);
canvas.drawRect(size.toRect(), Paint()..color = const Color(0xFF00D700));
}
@override
void update(double dt) {
velocity
..x = horizontalDirection * moveSpeed
..y = verticalDirection * moveSpeed;
position += velocity * dt;
if (corn != null) {
corn!.position = position;
}
super.update(dt);
}
@override
bool onKeyEvent(KeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
horizontalDirection = 0;
horizontalDirection += (keysPressed.contains(LogicalKeyboardKey.keyA) ||
keysPressed.contains(LogicalKeyboardKey.arrowLeft))
? -1
: 0;
horizontalDirection += (keysPressed.contains(LogicalKeyboardKey.keyD) ||
keysPressed.contains(LogicalKeyboardKey.arrowRight))
? 1
: 0;
verticalDirection = 0;
verticalDirection += (keysPressed.contains(LogicalKeyboardKey.keyW) ||
keysPressed.contains(LogicalKeyboardKey.arrowUp))
? -1
: 0;
verticalDirection += (keysPressed.contains(LogicalKeyboardKey.keyS) ||
keysPressed.contains(LogicalKeyboardKey.arrowDown))
? 1
: 0;
hasSpacePressed = keysPressed.contains(LogicalKeyboardKey.space);
if (hasSpacePressed) {
if (holdingCorn) {
dropCorn();
} else if (collisionCorn != null) {
pickUpCorn(collisionCorn!);
}
}
return true;
}
@override
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
if (other is Corn && intersectionPoints.isNotEmpty) {
print('Player collided with corn');
collisionCorn = other;
}
print(intersectionPoints);
super.onCollision(intersectionPoints, other);
}
@override
void onCollisionEnd(PositionComponent other) {
if (other is Corn) {
print('Player left corn');
collisionCorn = null;
}
super.onCollisionEnd(other);
}
void pickUpCorn(Corn corn) {
this.corn = corn;
}
void dropCorn() {
if (corn != null) {
corn!.position =
position + Vector2(0, size.y / 2); // Drop the corn below the player
corn = null;
}
}
}
class Corn extends PositionComponent with HasGameReference<MyGame> {
Corn() : super(size: Vector2(20, 20), anchor: Anchor.center);
@override
Future<void> onLoad() async {
position = game.size / 2;
add(RectangleHitbox());
await super.onLoad();
}
@override
void render(Canvas canvas) {
super.render(canvas);
canvas.drawRect(size.toRect(), Paint()..color = const Color(0xFFFFD700));
}
}
class MyGame extends FlameGame
with HasKeyboardHandlerComponents, HasCollisionDetection {
late Player player;
late Corn corn;
@override
Future<void> onLoad() async {
await super.onLoad();
player = Player();
add(player);
corn = Corn();
add(corn);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment