Skip to content

Commit

Permalink
- add trash body and sensor
Browse files Browse the repository at this point in the history
- added workflow back
  • Loading branch information
garlen-javier committed Feb 18, 2024
1 parent b1d49c6 commit 9fe8d04
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 39 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/gh-pages.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Gh-Pages

on:
push:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: subosito/flutter-action@v2
- uses: bluefireteam/flutter-gh-pages@v8
with:
baseHref: /ocean_cleanup/
webRenderer: canvaskit
File renamed without changes.
31 changes: 24 additions & 7 deletions lib/components/player/player.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:ocean_cleanup/components/trash/trash.dart';
import '../../bloc/player_movement/player_movement_barrel.dart';
import 'player_sprite.dart';

class Player extends BodyComponent {
class Player extends BodyComponent with ContactCallbacks{

final Vector2 pos;
final double width;
final double height;
Vector2 pos;
Vector2? scale;

Player(this.pos,this.width,this.height):super();
Player(this.pos,{this.scale}):super(){
scale = scale ?? Vector2.all(1);
}

Vector2 velocity = Vector2.zero();
double speed = 300;
Expand All @@ -18,6 +20,7 @@ class Player extends BodyComponent {
@override
Future<void> onLoad() async {
sprite = PlayerSprite();
sprite.scale = scale!;
await add(sprite);
await _initBlocListener();

Expand Down Expand Up @@ -55,11 +58,25 @@ class Player extends BodyComponent {
fixedRotation: true,
);

final shape = PolygonShape()..setAsBoxXY(width,height);
final fixtureDef = FixtureDef(
PolygonShape()..setAsBoxXY((sprite.width * 0.5) * scale!.x,(sprite.height * 0.5) * scale!.y),
isSensor: false,
);

final body = world.createBody(bodyDef);
body.createFixtureFromShape(shape);
body.createFixture(fixtureDef);
return body;
}

@override
void beginContact(Object other, Contact contact) {
if (other is Trash) {
Trash trash = other;
trash.removeFromParent();
}
super.beginContact(other, contact);
}



}
2 changes: 1 addition & 1 deletion lib/components/player/player_sprite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PlayerSprite extends SpriteAnimationGroupComponent with HasGameRef<GameSce

current = PlayerAnimationState.idle;
anchor = Anchor.center;
scale = Vector2.all(0.5);
//scale = Vector2.all(0.5);

priority = 2;
debugMode = true;
Expand Down
36 changes: 36 additions & 0 deletions lib/components/trash/trash.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:ocean_cleanup/components/trash/trash_sprite.dart';

class Trash extends BodyComponent {
final Vector2 pos;

Trash ({required this.pos});
late TrashSprite sprite;

@override
Future<void> onLoad() async {
sprite = TrashSprite();
await add(sprite);
return super.onLoad();
}

@override
Body createBody() {
final bodyDef = BodyDef(
type: BodyType.static,
userData: this,
position: pos,
);

final fixtureDef = FixtureDef(
PolygonShape()..setAsBoxXY(sprite.width * 0.5,sprite.height * 0.5),
isSensor: true,
);

final body = world.createBody(bodyDef);
body.createFixture(fixtureDef);

//renderBody = false;
return body;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,29 @@ import '../../scenes/game_scene.dart';

class TrashSprite extends SpriteAnimationGroupComponent with HasGameRef<GameScene> {

final Vector2 pos;
TrashSprite(this.pos) :super();
final Vector2 spriteSize = Vector2(26,64);
//final Vector2 pos;
TrashSprite() :super();

@override
FutureOr<void> onLoad() async {
final spritesheet = SpriteSheet(
image: gameRef.images.fromCache("recycle_items.png"),
srcSize: Vector2(26,64)
srcSize: spriteSize
);

final trashSprite = SpriteComponent(
sprite: spritesheet.getSprite(0, 1),
position: pos,
anchor: Anchor.center,
scale: Vector2.all(0.8),
priority: 1
);

scale = Vector2.all(0.8);
anchor = Anchor.center;
//position = pos;
priority = 1;
size = spriteSize;

add(trashSprite);
//debugMode = true;
debugMode = true;
return super.onLoad( );
}

Expand Down
7 changes: 4 additions & 3 deletions lib/scenes/game_scene.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class GameScene extends Forge2DGame {
}

Future<void> loadWorlds() async {
FlutterView view = WidgetsBinding.instance.platformDispatcher.views.first;
final gameWorld = GameWorld(playerMovementBloc:playerMovementBloc);
final gameCamera = CameraComponent.withFixedResolution(
width: GameWorld.worldSize.width,
Expand All @@ -50,17 +51,17 @@ class GameScene extends Forge2DGame {
..position = Vector2(GameWorld.worldSize.width, GameWorld.worldSize.height);
//..anchor = Anchor.topLeft;

FlutterView view = WidgetsBinding.instance.platformDispatcher.views.first;
final hudWorld = HudWorld(playerMovementBloc:playerMovementBloc);
final hudCamera = CameraComponent.withFixedResolution(
width: view.physicalSize.width / view.devicePixelRatio,
height: view.physicalSize.height / view.devicePixelRatio,
world: hudWorld,
);

//final hud = Hud(playerMovementBloc:playerMovementBloc);

//final hud = Hud(playerMovementBloc:playerMovementBloc);
//await addAll([gameWorld,gameCamera,hud]);
await addAll([gameWorld,gameCamera,hudWorld,hudCamera]);
await addAll([gameWorld,gameCamera, hudWorld ,hudCamera]);
//_zoomFollowPlayer(gameCamera, gameWorld.player);
}

Expand Down
40 changes: 24 additions & 16 deletions lib/worlds/game_world.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import 'dart:async';
import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame/experimental.dart';
import 'package:flame/flame.dart';
import 'package:flame/game.dart';
import 'package:flame_bloc/flame_bloc.dart';
import 'package:flame_forge2d/flame_forge2d.dart';
import 'package:flame_tiled/flame_tiled.dart';
import 'package:flutter/material.dart';
import '../bloc/player_movement/player_movement_bloc.dart';
import '../bloc/player_movement/player_movement_state.dart';
import '../components/physics/brick_body.dart';
import '../components/brick/brick_body.dart';
import '../components/player/player.dart';
import 'package:flame/src/camera/world.dart' as camWorld;
import '../components/trash/trash.dart';
import '../components/trash/trash_sprite.dart';

import '../components/trash_sprite.dart';

///Used Forge2DWorld incase we need to add levels with collision
class GameWorld extends Forge2DWorld
Expand All @@ -37,7 +37,7 @@ class GameWorld extends Forge2DWorld

await _initLevel();

player = Player(Vector2(worldSize.width ,worldSize.height ), 32,32);
player = Player(Vector2(worldSize.width ,worldSize.height),scale: Vector2.all(0.5));
await _addPlayerBloc(player);
await add(player);

Expand Down Expand Up @@ -70,9 +70,9 @@ class GameWorld extends Forge2DWorld
{
for(var col in objGroup.objects)
{
TrashSprite trash = TrashSprite(Vector2(col.x + 16,col.y + 16));
Trash trash = Trash(pos:Vector2(col.x + 16,col.y + 16));
add(trash);
trashes.add(trash);
//trashes.add(trash);
}
}
}
Expand All @@ -97,20 +97,28 @@ class GameWorld extends Forge2DWorld
void update(double dt) {
if(player != null)
{
for(var trash in trashes)
{
if(player.containsPoint(trash.position))
{
print("Collide");
}
}
// print("player pos " + player.position.toString());
// print("player widht " + player.height.toString());
// for(var trash in trashes)
// {
// //print("trash " + trash.width.toString());
// if(isCollide(trash))
// {
// print("Collide");
// trash.removeFromParent();
// }
//
// // if(player.containsLocalPoint(trash.position))
// // {
// // print("Collide");
// // }
// }
}



super.update(dt);
}





}
16 changes: 12 additions & 4 deletions lib/worlds/hud_world.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import 'dart:async';
import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flame/input.dart';
import 'package:flutter/cupertino.dart' as cupertino;
import '../bloc/player_movement/player_movement_bloc.dart';
import '../scenes/game_scene.dart';

Expand All @@ -12,9 +14,11 @@ class HudWorld extends World with HasGameRef<GameScene>
HudWorld({required this.playerMovementBloc}):super();

late final JoystickComponent _joystick;
late Vector2 _gameSize;

@override
FutureOr<void> onLoad() async{
_gameSize = gameRef!.size;
await _showJoyStick();
await _showFPSDisplay();
return super.onLoad();
Expand All @@ -35,18 +39,16 @@ class HudWorld extends World with HasGameRef<GameScene>
background: SpriteComponent(
sprite: joyStickBase ,
),
position: Vector2(-gameRef!.size.x * 0.4,gameRef!.size.y * 0.35),
priority: 4,
position: Vector2(-_gameSize.x * 0.4,_gameSize.y * 0.35),
);
_joystick.scale = Vector2.all(0.7);
await component.add(_joystick);
await add(component);
// await add(_joystick);
}

Future<void> _showFPSDisplay() async {
await add(FpsTextComponent(
position: Vector2(gameRef!.size.x * 0.35,gameRef!.size.y * 0.4),
position: Vector2(_gameSize.x * 0.35,_gameSize.y * 0.4),
));
}

Expand All @@ -57,5 +59,11 @@ class HudWorld extends World with HasGameRef<GameScene>
playerMovementBloc.move(velDir);
}

@override
void onGameResize(Vector2 size) {
_gameSize = size;
super.onGameResize(size);
}


}

0 comments on commit 9fe8d04

Please sign in to comment.