Skip to content

Commit

Permalink
Merge pull request #2 from Schnurber/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Schnurber authored Apr 13, 2021
2 parents 8a800f5 + 3c65a3b commit 7d82661
Show file tree
Hide file tree
Showing 26 changed files with 584 additions and 520 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ It is a game written in flutter and dart.

There are no comments inside because all the explainations are inside the book.

The book is available soon in german language at Amazon:
The book is available in german language at Amazon:

[German Book](https://www.amazon.de/dp/3110690640/ref=cm_sw_em_r_mt_dp_U_TF4xEbN4AZSA7)

Expand Down
Binary file modified assets/images/logoChicken.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/tiles/enemy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions lib/AboutPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AboutPage extends StatelessWidget {
return themed(
context,
Scaffold(
appBar: AppBar(title: Text(Lang.of(context).t("About"))),
appBar: AppBar(title: Text(Lang.of(context)!.t("About"))),
drawer: buildDrawer(context, route, game.prefs, game),
body: _about(context)));
}
Expand All @@ -29,7 +29,7 @@ class AboutPage extends StatelessWidget {
text: TextSpan(
style: TextStyle(
fontFamily: gameFont, fontSize: 16 * getTextScale(context)),
text: Lang.of(context).t('info'),
text: Lang.of(context)!.t('info'),
),
softWrap: true,
),
Expand Down
77 changes: 45 additions & 32 deletions lib/Animal.dart
Original file line number Diff line number Diff line change
@@ -1,59 +1,72 @@
import 'package:flutter/material.dart';
import 'package:chicken_maze/stuff/Vect2.dart';
import 'package:flame/animation.dart' as animation;
import 'package:flame/position.dart';
import 'package:flame/sprite.dart';
import 'package:flame/components.dart';
import 'package:chicken_maze/stuff/constants.dart';
import 'package:chicken_maze/ChickenGame.dart';

abstract class Animal {
SpriteAnimation animationRight;
SpriteAnimation animationLeft;
SpriteAnimation animationUp;
SpriteAnimation animationDown;
SpriteAnimation animationIdle;
late SpriteAnimation currentAnimation;

animation.Animation animationRight;
animation.Animation animationLeft;
animation.Animation animationUp;
animation.Animation animationDown;
animation.Animation animationIdle;
animation.Animation currentAnimation;

Position pos;
Vect2<int> screenPos;
Position targetPos;
Vect2<int> mapPos;
late Vect2<double> pos;
late Vect2<int> screenPos;
late Vect2<double> targetPos;
late Vect2<int> mapPos;
ChickenGame game;

Animal(this.game, {this.mapPos,
this.animationLeft,
this.animationRight,
this.animationUp,
this.animationDown,
this.animationIdle}) {
Animal(this.game,
{required this.mapPos,
required this.animationLeft,
required this.animationRight,
required this.animationUp,
required this.animationDown,
required this.animationIdle}) {
initPos(1, 0);
currentAnimation= animationIdle;
currentAnimation = animationIdle;
}

void initPos(int x, int y) {
mapPos = Vect2<int>(x,y);
targetPos = Position(0.0 + raster * mapPos.x, 0.0 + raster * mapPos.y);
mapPos = Vect2<int>(x, y);
targetPos = Vect2<double>(raster * mapPos.x, raster * mapPos.y);
screenPos = Vect2<int>(mapPos.x, mapPos.y);
pos = Position( targetPos.x, targetPos.y);
pos = Vect2<double>(targetPos.x, targetPos.y);
}

void render(Canvas canvas) {
//Render Animal
if (currentAnimation == animationIdle) {
currentAnimation.update(0.1);
}
currentAnimation.getSprite().renderPosition(canvas, pos);
currentAnimation
.getSprite()
.render(canvas, position: Vector2(pos.x, pos.y));
}

void move(Direction dir) {
int xp = 0;
int yp = 0;
switch(dir) {
case Direction.left: xp = -1; break;
case Direction.right: xp = 1; break;
case Direction.up: yp = -1; break;
case Direction.down: yp = 1; break;
case Direction.none: xp = 0; yp = 0; break;
switch (dir) {
case Direction.left:
xp = -1;
break;
case Direction.right:
xp = 1;
break;
case Direction.up:
yp = -1;
break;
case Direction.down:
yp = 1;
break;
case Direction.none:
xp = 0;
yp = 0;
break;
}
targetPos.x += xp * raster;
targetPos.y += yp * raster;
Expand All @@ -62,8 +75,8 @@ abstract class Animal {
}

// Abstract

void update([Direction direction]);

void sound();
}
}
71 changes: 40 additions & 31 deletions lib/Chicken.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,100 @@ import 'package:chicken_maze/ChickenGame.dart';
import 'package:chicken_maze/Animal.dart';
import 'package:chicken_maze/stuff/constants.dart';
import 'package:chicken_maze/stuff/Vect2.dart';
import 'package:flame/position.dart';
import 'package:flame/components.dart';
import 'dart:async';

class Chicken extends Animal {

Chicken(ChickenGame game) : super(game,
animationLeft: AssetLoader.chickenAnimationLeft,
animationRight: AssetLoader.chickenAnimationRight,
animationUp: AssetLoader.chickenAnimationUp,
animationDown: AssetLoader.chickenAnimationDown,
animationIdle: AssetLoader.chickenAnimationIdle) {
Chicken(ChickenGame game)
: super(game,
mapPos: Vect2<int>(1, 0),
animationLeft: AssetLoader.chickenAnimationLeft,
animationRight: AssetLoader.chickenAnimationRight,
animationUp: AssetLoader.chickenAnimationUp,
animationDown: AssetLoader.chickenAnimationDown,
animationIdle: AssetLoader.chickenAnimationIdle) {
lives = 3;
canKill = 0;
}

Timer timer;
int lives ;
int canKill;
Timer? timer;
late int lives;
late int canKill;

void beamToPos(int x, int y) {
mapPos = Vect2<int>(x, y);
int px = (game.maze.screenTileDimensions.x / 2).floor();
int py = (game.maze.screenTileDimensions.y / 2).floor();
screenPos = Vect2<int>(px, py);
targetPos = Position(raster * px, raster * py);
pos = Position( targetPos.x, targetPos.y);
targetPos = Vect2<double>(raster * px, raster * py);
pos = Vect2<double>(targetPos.x, targetPos.y);
game.direction = Direction.none;
game.maze.bgrTilePos = Vect2<int>(-x + px, -y + py);
game.maze.bgrPos = Position(raster * game.maze.bgrTilePos.x, raster * game.maze.bgrTilePos.y);
game.maze.bgrTargetPos = Position( game.maze.bgrPos.x, game.maze.bgrPos.y);
game.maze.bgrPos = Vector2(
raster * game.maze.bgrTilePos.x, raster * game.maze.bgrTilePos.y);
game.maze.bgrTargetPos = Vector2(game.maze.bgrPos.x, game.maze.bgrPos.y);
}

@override
@override
void update([Direction direction = Direction.none]) {
if (direction == Direction.right) {
currentAnimation = animationRight;
if (pos.x < targetPos.x) pos.x += chickenSpeed;
if (game.maze.bgrPos.x > game.maze.bgrTargetPos.x) game.maze.bgrPos.x -= chickenSpeed;
if (pos.x >= targetPos.x && game.maze.bgrPos.x <= game.maze.bgrTargetPos.x) {
if (game.maze.bgrPos.x > game.maze.bgrTargetPos.x)
game.maze.bgrPos.x -= chickenSpeed;
if (pos.x >= targetPos.x &&
game.maze.bgrPos.x <= game.maze.bgrTargetPos.x) {
game.direction = Direction.none;
idle();
game.maze.checkGrain(mapPos.x, mapPos.y);
}
} else if (direction == Direction.left) {
currentAnimation = animationLeft;
if (pos.x > targetPos.x) pos.x -= chickenSpeed;
if (game.maze.bgrPos.x < game.maze.bgrTargetPos.x) game.maze.bgrPos.x += chickenSpeed;
if (pos.x <= targetPos.x && game.maze.bgrPos.x >= game.maze.bgrTargetPos.x) {
if (game.maze.bgrPos.x < game.maze.bgrTargetPos.x)
game.maze.bgrPos.x += chickenSpeed;
if (pos.x <= targetPos.x &&
game.maze.bgrPos.x >= game.maze.bgrTargetPos.x) {
game.direction = Direction.none;
idle();
game.maze.checkGrain(mapPos.x, mapPos.y);
}
} else if (direction == Direction.down) {
currentAnimation = animationDown;
if (pos.y < targetPos.y) pos.y += chickenSpeed;
if (game.maze.bgrPos.y > game.maze.bgrTargetPos.y) game.maze.bgrPos.y -= chickenSpeed;
if (pos.y >= targetPos.y && game.maze.bgrPos.y <= game.maze.bgrTargetPos.y) {
if (game.maze.bgrPos.y > game.maze.bgrTargetPos.y)
game.maze.bgrPos.y -= chickenSpeed;
if (pos.y >= targetPos.y &&
game.maze.bgrPos.y <= game.maze.bgrTargetPos.y) {
game.direction = Direction.none;
idle();
game.maze.checkGrain(mapPos.x, mapPos.y);
}
} else if (direction == Direction.up) {
currentAnimation = animationUp;
if (pos.y > targetPos.y) pos.y -= chickenSpeed;
if (game.maze.bgrPos.y < game.maze.bgrTargetPos.y) game.maze.bgrPos.y += chickenSpeed;
if (pos.y <= targetPos.y && game.maze.bgrPos.y >= game.maze.bgrTargetPos.y) {
if (game.maze.bgrPos.y < game.maze.bgrTargetPos.y)
game.maze.bgrPos.y += chickenSpeed;
if (pos.y <= targetPos.y &&
game.maze.bgrPos.y >= game.maze.bgrTargetPos.y) {
game.direction = Direction.none;
idle();
game.maze.checkGrain(mapPos.x, mapPos.y);
}
}
}

void idle() {
if (timer != null) {
timer.cancel();
}
timer = Timer(Duration(milliseconds: 2000), () {
timer?.stop();
timer = Timer(2000, callback: () {
if (game.direction == Direction.none) {
currentAnimation = animationIdle;
}
});
}
@override

@override
void sound() {
AssetLoader.cluck();
}
}
}
Loading

0 comments on commit 7d82661

Please sign in to comment.