Skip to content

Commit

Permalink
doc: Added some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMitterer committed Dec 28, 2015
1 parent 7173958 commit 05e96d8
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
doc/api/
4 changes: 2 additions & 2 deletions lib/spaceinvaders.dart
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */


/// The classic Space Invaders game written in Dart.
library spaceinvaders; library spaceinvaders;


import 'dart:collection'; import 'dart:collection';
import 'dart:html' as dom; import 'dart:html' as dom;
import 'dart:math' as math; import 'dart:math' as math;


import 'package:console_log_handler/console_log_handler.dart';
import 'package:logging/logging.dart'; import 'package:logging/logging.dart';


part 'spaceinvaders/drawables.dart'; part 'spaceinvaders/drawables.dart';
Expand Down
2 changes: 2 additions & 0 deletions lib/spaceinvaders/FrameHandler.dart
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ part of spaceinvaders;


enum Direction { Left, Right } enum Direction { Left, Right }


/// Calls the [update]-Function every x-frames.
/// The the intervall is defined via [updateFrequency]
class FrameHandler { class FrameHandler {
int _frames = 0; int _frames = 0;
int _frequency = 30; int _frequency = 30;
Expand Down
2 changes: 2 additions & 0 deletions lib/spaceinvaders/InputHandler.dart
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


part of spaceinvaders; part of spaceinvaders;


/// Abstraction for keycodes typed by the user
class KeyCode { class KeyCode {
final int code; final int code;
const KeyCode(this.code); const KeyCode(this.code);
Expand All @@ -32,6 +33,7 @@ class KeyCode {
static const KeyCode Space = const KeyCode(32); static const KeyCode Space = const KeyCode(32);
} }


/// Listens for key pressed
class InputHandler { class InputHandler {
final Logger _logger = new Logger('spaceinvaders.Sprites'); final Logger _logger = new Logger('spaceinvaders.Sprites');


Expand Down
2 changes: 2 additions & 0 deletions lib/spaceinvaders/Magazin.dart
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@


part of spaceinvaders; part of spaceinvaders;


/// Magazin holds and fires all the [Bullet]s fire by either an [Alien]
/// or by the [Tank]
class Magazin { class Magazin {
final List<Bullet> _bullets = new List<Bullet>(); final List<Bullet> _bullets = new List<Bullet>();


Expand Down
23 changes: 23 additions & 0 deletions lib/spaceinvaders/drawables.dart
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,37 +19,50 @@


part of spaceinvaders; part of spaceinvaders;


/// Base class for all the drawable (paintable) objects on the screen.
///
/// [Painter] uses this class for doing it's job
abstract class Drawable { abstract class Drawable {
/// The image that represents this [Drawable]
Sprite get sprite; Sprite get sprite;

int get x; int get x;
int get y; int get y;


void draw(final Painter painter); void draw(final Painter painter);
} }


/// Mixin to move an object horizontally
class MoveHorizontale { class MoveHorizontale {
int x = 0; int x = 0;
void moveLeft({ final int speed: 1}) { x -= speed; } void moveLeft({ final int speed: 1}) { x -= speed; }
void moveRight({ final int speed: 1}) { x += speed; } void moveRight({ final int speed: 1}) { x += speed; }
} }


/// Mixin to move an object vertically
class MoveVertical{ class MoveVertical{
int y = 0; int y = 0;
void moveUp({ final int speed: 1}) { y -= speed; } void moveUp({ final int speed: 1}) { y -= speed; }
void moveDown({ final int speed: 1}) { y += speed; } void moveDown({ final int speed: 1}) { y += speed; }
} }


/// More or less a base implementation of [Drawable] plus
/// with + height.
abstract class ScreenObject implements Drawable { abstract class ScreenObject implements Drawable {


void draw(final Painter painter) { painter.draw(this); } void draw(final Painter painter) { painter.draw(this); }


int get width => sprite._width; int get width => sprite._width;
int get height => sprite._height; int get height => sprite._height;


/// Describes the [Drawable] as [Rectangle]
math.Rectangle<num> get rect => new math.Rectangle<num>(x,y,width,height); math.Rectangle<num> get rect => new math.Rectangle<num>(x,y,width,height);

/// Collision detection
bool collidesWith(final ScreenObject so) => rect.intersects(so.rect); bool collidesWith(final ScreenObject so) => rect.intersects(so.rect);
} }


/// The Tank can move left and right and shoots the [Alien]s
class Tank extends ScreenObject with MoveHorizontale,MoveVertical implements Drawable { class Tank extends ScreenObject with MoveHorizontale,MoveVertical implements Drawable {
final Logger _logger = new Logger('spaceinvaders.Tank'); final Logger _logger = new Logger('spaceinvaders.Tank');


Expand All @@ -65,6 +78,7 @@ class Tank extends ScreenObject with MoveHorizontale,MoveVertical implements Dra
Sprite get sprite => _sprite; Sprite get sprite => _sprite;
} }


/// The enemy - the Aliens are organized in a [Swarm]
class Alien extends ScreenObject implements Drawable { class Alien extends ScreenObject implements Drawable {
final Logger _logger = new Logger('spaceinvaders.Alien'); final Logger _logger = new Logger('spaceinvaders.Alien');


Expand Down Expand Up @@ -92,6 +106,7 @@ class Alien extends ScreenObject implements Drawable {
} }
} }


/// Collection of [Alien]s
class Swarm extends ScreenObject implements Drawable { class Swarm extends ScreenObject implements Drawable {
final Logger _logger = new Logger('spaceinvaders.Swarm'); final Logger _logger = new Logger('spaceinvaders.Swarm');


Expand Down Expand Up @@ -189,6 +204,7 @@ class Swarm extends ScreenObject implements Drawable {
return new UnmodifiableListView<Alien>(aliens.where((final Alien alien) => !alien.killed)); return new UnmodifiableListView<Alien>(aliens.where((final Alien alien) => !alien.killed));
} }


/// Looks for the frontmost [Alien]
Alien closestAlien() { Alien closestAlien() {
Alien closest; Alien closest;
int yPos = 0; int yPos = 0;
Expand Down Expand Up @@ -227,6 +243,7 @@ class Swarm extends ScreenObject implements Drawable {
} }
} }


/// City can be destroyed by [Alien]
class City extends ScreenObject implements Drawable { class City extends ScreenObject implements Drawable {
final Logger _logger = new Logger('spaceinvaders.City'); final Logger _logger = new Logger('spaceinvaders.City');
int x = 0; int x = 0;
Expand All @@ -241,6 +258,10 @@ class City extends ScreenObject implements Drawable {
int get height => _sprite._height; int get height => _sprite._height;
} }


/// Holds (and draws!!!!) the [City] objects
///
/// Cities are drawn on [ImagePainter]. This makes it possible
/// to destroy parts of a [City]
class Cities extends ScreenObject implements Drawable { class Cities extends ScreenObject implements Drawable {
final Logger _logger = new Logger('spaceinvaders.Cities'); final Logger _logger = new Logger('spaceinvaders.Cities');


Expand Down Expand Up @@ -351,6 +372,8 @@ class Cities extends ScreenObject implements Drawable {


} }


/// Bullet fired either by an [Alien] or by the [Tank].
/// A [Magazin] is used to hold the [Bullet]s
class Bullet extends ScreenObject with MoveVertical implements Drawable { class Bullet extends ScreenObject with MoveVertical implements Drawable {
final Logger _logger = new Logger('spaceinvaders.Bullet'); final Logger _logger = new Logger('spaceinvaders.Bullet');


Expand Down
1 change: 1 addition & 0 deletions web/app/config.dart
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


part of spaceinvaders.app; part of spaceinvaders.app;


/// Minimal Log-Configuration
void configLogging() { void configLogging() {
hierarchicalLoggingEnabled = false; // set this to true - its part of Logging SDK hierarchicalLoggingEnabled = false; // set this to true - its part of Logging SDK


Expand Down
2 changes: 2 additions & 0 deletions web/app/init.dart
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@


part of spaceinvaders.app; part of spaceinvaders.app;


/// Initializes the game - means sets the initial values for [FrameHandler] and so forth.
/// Defines the base-positions for all the [Drawables] like [Tank], [Swarm]...
void init(final FrameHandler frameHandler, final ScreenSize screensize, final SpriteFactory spritefactory) { void init(final FrameHandler frameHandler, final ScreenSize screensize, final SpriteFactory spritefactory) {
frameHandler.updateFrequency = 30; frameHandler.updateFrequency = 30;


Expand Down
3 changes: 3 additions & 0 deletions web/app/render.dart
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@


part of spaceinvaders.app; part of spaceinvaders.app;


/// Draws all the [Drawables]. [Screen] returns a [Painter] that is used for
/// all the drawing operations
void render(final Screen screen,final SpriteFactory spritefactory) { void render(final Screen screen,final SpriteFactory spritefactory) {
screen.clear(); screen.clear();


final Painter painter = screen.painter; final Painter painter = screen.painter;


spritefactory.tank.draw(painter); spritefactory.tank.draw(painter);

spritefactory.swarm.draw(painter); spritefactory.swarm.draw(painter);


spritefactory.cities.draw(painter); spritefactory.cities.draw(painter);
Expand Down
3 changes: 3 additions & 0 deletions web/app/update.dart
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@


part of spaceinvaders.app; part of spaceinvaders.app;


/// All the properties will be updatet. X/Y positions, moving the swarm,
/// firing [Bullet]s from the [Magazin]
bool update(final FrameHandler frameHandler,final SpriteFactory spritefactory, bool update(final FrameHandler frameHandler,final SpriteFactory spritefactory,
final InputHandler inputHandler, final ScreenSize screensize) { final InputHandler inputHandler, final ScreenSize screensize) {


Expand Down Expand Up @@ -105,6 +107,7 @@ bool update(final FrameHandler frameHandler,final SpriteFactory spritefactory,
} }


// Moves the sprites down (toggle) // Moves the sprites down (toggle)
// The frequency is defined in "init"
frameHandler.update((final Direction direction) { frameHandler.update((final Direction direction) {
spritefactory.swarm.toggle(); spritefactory.swarm.toggle();
direction == Direction.Left ? spritefactory.swarm.moveLeft() : spritefactory.swarm.moveRight(); direction == Direction.Left ? spritefactory.swarm.moveLeft() : spritefactory.swarm.moveRight();
Expand Down
Binary file added web/images/invaders-copy2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 05e96d8

Please sign in to comment.