-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
155 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import 'package:flame/anchor.dart'; | ||
import 'package:flame/components/component.dart'; | ||
import 'package:flame/sprite.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
|
||
/// Renders a single sprite based on [SpriteComponent] on the game board. | ||
class BoardComponent extends SpriteComponent { | ||
/// Convenient constructor. | ||
BoardComponent(String fileName, double tileSize) { | ||
width = tileSize; | ||
height = tileSize; | ||
sprite = Sprite(fileName); | ||
anchor = Anchor.center; | ||
} | ||
|
||
@override | ||
void render(Canvas canvas) { | ||
canvas.save(); | ||
canvas.translate(width * 0.5, height * 0.5); | ||
super.render(canvas); | ||
canvas.restore(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import 'dart:math'; | ||
import 'dart:ui'; | ||
|
||
import 'package:flame/components/component.dart'; | ||
|
||
import '../models/snake.dart'; | ||
import '../models/vec2d.dart'; | ||
import '../renderer/board_component.dart'; | ||
|
||
/// Renders the snake using [SpriteComponent] for each part of it. | ||
class SnakeComponent extends Component { | ||
/// Convenient constructor. | ||
SnakeComponent(this.tileSize); | ||
|
||
/// The tile size. Use to draw the snake. | ||
final double tileSize; | ||
|
||
List<BoardComponent> _snakeBody; | ||
|
||
BoardComponent _buildHead(Vec2d current, Vec2d prev) { | ||
final imageRotation = pi / 2; | ||
final sameAxis = prev.x == current.x; | ||
|
||
final mustInvest = atan2(current.x - prev.x, current.y - prev.y) <= 0.0; | ||
|
||
return BoardComponent('snake/head.png', tileSize) | ||
..x = tileSize * current.x | ||
..y = tileSize * current.y | ||
..angle = (sameAxis ? 0.0 : imageRotation) + (mustInvest ? pi : 0.0); | ||
} | ||
|
||
BoardComponent _buildTail(Vec2d current, Vec2d prev) { | ||
final imageRotation = pi / 2; | ||
final sameAxis = prev.x == current.x; | ||
|
||
final mustInvest = atan2(current.x - prev.x, current.y - prev.y) > 0.0; | ||
|
||
return BoardComponent('snake/tail.png', tileSize) | ||
..x = tileSize * current.x | ||
..y = tileSize * current.y | ||
..angle = (sameAxis ? 0.0 : imageRotation) + (mustInvest ? pi : 0.0); | ||
} | ||
|
||
BoardComponent _buildBody(Vec2d current, Vec2d prev, Vec2d next) { | ||
final imageRotation = pi / 2; | ||
|
||
BoardComponent component; | ||
|
||
final diffAngle = atan2(next.x - current.x, next.y - current.y) - | ||
atan2(prev.x - current.x, prev.y - current.y); | ||
|
||
if (asin(sin(diffAngle)).abs() == (pi / 2)) { | ||
final isClockwise = asin(sin(diffAngle)) < 0.0; | ||
final rotate = isClockwise ? 0.0 : imageRotation; | ||
|
||
component = BoardComponent('snake/body_curve.png', tileSize); | ||
component.angle = -atan2(next.x - current.x, next.y - current.y) + rotate; | ||
} else { | ||
component = BoardComponent('snake/body.png', tileSize); | ||
final sameAxis = prev.x == current.x; | ||
component.angle = sameAxis ? 0.0 : imageRotation; | ||
} | ||
|
||
return component | ||
..x = tileSize * current.x | ||
..y = tileSize * current.y; | ||
} | ||
|
||
/// Update the snake position using [snake] info. | ||
void updateSnake(Snake snake) { | ||
if (snake != null) { | ||
final snakeBody = snake.body.toList(); | ||
_snakeBody = <BoardComponent>[ | ||
_buildHead( | ||
snakeBody[0], | ||
snakeBody[1], | ||
), | ||
]; | ||
|
||
for (var i = 1; i <= snakeBody.length - 2; i++) { | ||
_snakeBody.add( | ||
_buildBody( | ||
snakeBody[i], | ||
snakeBody[i - 1], | ||
snakeBody[i + 1], | ||
), | ||
); | ||
} | ||
|
||
_snakeBody.add(_buildTail( | ||
snakeBody.last, | ||
snakeBody[snakeBody.length - 2], | ||
)); | ||
} else { | ||
_snakeBody = null; | ||
} | ||
} | ||
|
||
@override | ||
void render(Canvas canvas) { | ||
if (_snakeBody != null) { | ||
for (var part in _snakeBody.skip(1)) { | ||
part.render(canvas); | ||
} | ||
|
||
_snakeBody.first.render(canvas); | ||
} | ||
} | ||
|
||
@override | ||
void update(double dt) {} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters