Permalink
Browse files

doc: Move most classes to lib

  • Loading branch information...
MikeMitterer committed Dec 18, 2015
1 parent 003ba15 commit 71739581efde1e085b433bd03cfc97bb2e662857
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2015, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

library spaceinvaders;

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

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

part 'spaceinvaders/drawables.dart';
part 'spaceinvaders/painter.dart';
part 'spaceinvaders/screen.dart';
part 'spaceinvaders/sprite.dart';

part 'spaceinvaders/FrameHandler.dart';
part 'spaceinvaders/InputHandler.dart';
part 'spaceinvaders/Magazin.dart';
part 'spaceinvaders/SpeedGenerator.dart';
part 'spaceinvaders/SpriteFactory.dart';



@@ -0,0 +1,43 @@
/*
* Copyright (c) 2015, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

part of spaceinvaders;

enum Direction { Left, Right }

class FrameHandler {
int _frames = 0;
int _frequency = 30;
Direction _direction = Direction.Right;

void update(void updater(final Direction direction)) {
_frames++;
if(_frames % _frequency == 0) {
updater(_direction);
}
}

void set updateFrequency(final int frequency) {
_frequency = math.max(1,frequency);
}

int get updateFrequency => _frequency;

void toggleDirection() { _direction = _direction == Direction.Left ? Direction.Right : Direction.Left; }
}
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2015, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

part of spaceinvaders;

class KeyCode {
final int code;
const KeyCode(this.code);

static const KeyCode Left = const KeyCode(37);
static const KeyCode Right = const KeyCode(39);

static const KeyCode Up = const KeyCode(38);
static const KeyCode Down = const KeyCode(40);

static const KeyCode Space = const KeyCode(32);
}

class InputHandler {
final Logger _logger = new Logger('spaceinvaders.Sprites');

final Map<int,bool> _down = new Map<int,bool>();
final Map<int,bool> _pressed = new Map<int,bool>();

void create() {
dom.document.onKeyDown.listen((final dom.KeyboardEvent event) {
_down[event.keyCode] = true;
_pressed[event.keyCode] = true;

//_logger.info("K ${event.keyCode}");
});
dom.document.onKeyUp.listen((final dom.KeyboardEvent event) {
_down[event.keyCode] = false;
_pressed[event.keyCode] = false;
});
}

/// Always returns the current [KeyCode] status
bool isDown(final KeyCode key) {
return _down.containsKey(key.code) && _down[key.code];
}

/// Forces the user to press and release this [KeyCode] for example the
/// space bare for firing
bool isPressed(final KeyCode key) {
if(_pressed.containsKey(key.code)) {
final bool status = _pressed[key.code];
_pressed[key.code] = false;

return status;
}
return false;
}
}
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2015, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

part of spaceinvaders;

class Magazin {
final List<Bullet> _bullets = new List<Bullet>();

void addBullet(final Bullet bullet) {
_bullets.add(bullet);
}

// remove bullets outside of the canvas
void removeFailedBullets(int maxHeight) {
_bullets.removeWhere((final Bullet bullet) {
return bullet.y > maxHeight || bullet.y < 0;
});
}

void fire() {
_bullets.forEach((final Bullet bullet) => bullet.move());
}

void draw(final Painter painter) {
painter.save();
_bullets.forEach((final Bullet bullet) => bullet.draw(painter));
painter.restore();
}

void checkIfAlienIsHit(final UnmodifiableListView<Alien> aliens) {
aliens.where((final Alien alien) => !alien.killed).forEach((final Alien alien) {

final math.Rectangle rectAlien = new math.Rectangle(alien.x,alien.y,alien.width,alien.height);

for(int index = 0;index < _bullets.length;index++) {
final Bullet bullet = _bullets[index];
final math.Rectangle rectBullet = new math.Rectangle(bullet.x,bullet.y,bullet.width,bullet.height);
if(rectAlien.intersects(rectBullet)) {
alien.killed = true;
_bullets.remove(bullet);
break;
}
};
});
}

void checkIfCityIsHit(final Cities cities) {
for(int index = 0;index < _bullets.length;index++) {
final Bullet bullet = _bullets[index];
if(cities.isCityHit(bullet)) {
_bullets.remove(bullet);
break;
}
}
}

void checkIfTankIsHit(final Tank tank) {
for(int index = 0;index < _bullets.length;index++) {
final Bullet bullet = _bullets[index];
if(tank.collidesWith(bullet)) {
tank.hits++;
_bullets.remove(bullet);
break;
}
}
}
}
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2015, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

part of spaceinvaders;

/// Implemented as Singleton - makes it easy (+ cheap) to create this
/// object on every update iteration
class SpeedGenerator {
final Logger _logger = new Logger('spaceinvaders.SpeedGenerator');
final math.Random _random = new math.Random();

static final SpeedGenerator _speedgenerator = new SpeedGenerator._internal();
factory SpeedGenerator() => _speedgenerator;

int getSpeed(final int min,final int max) {
var speed = _random.nextInt(max - (max - min))+(max - min) + 1;
//_logger.info("S $speed");
return speed;
}

SpeedGenerator._internal();
}
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2015, Michael Mitterer (office@mikemitterer.at),
* IT-Consulting and Development Limited.
*
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

part of spaceinvaders;

/// Creates all the [Sprite]s
///
/// If follows the Factory-Pattern. If the requested object does not exist it will be created.
/// If it exists it returns the already existing instance.
///
/// init() {
/// final Tank tank = spritefactory.tank;
///
/// ...
/// if (inputHandler.isDown(KeyCode.Left)) {
///
/// tank.moveLeft(speed: tankSpeed);
/// }
/// }
class SpriteFactory {
final Logger _logger = new Logger('spaceinvaders.SpriteFactory');

final dom.ImageElement _image = new dom.ImageElement(src: "images/invaders.png");

final List<ToggleSprite> _alienSprites = new List<ToggleSprite>();

Tank _tank;
Swarm _swarm;
Magazin _magazin;
Cities _cities;

void create() {
if (_alienSprites.isEmpty) {

// blue one
_alienSprites.add(new ToggleSprite(new Sprite(_image,0, 0, 22, 16),new Sprite(_image,0, 16, 22, 16)));

// pink Alien
_alienSprites.add(new ToggleSprite(new Sprite(_image,22, 0, 16, 16),new Sprite(_image,22, 16, 16, 16)));

// light blue Alien
_alienSprites.add(new ToggleSprite(new Sprite(_image,38, 0, 24, 16),new Sprite(_image,38, 16, 24, 16)));
}
}

Tank get tank => _tank ?? (_tank = new Tank(new Sprite(_image, 62, 0, 22, 16)));

Swarm get swarm => _swarm ?? ( _swarm = new Swarm(new UnmodifiableListView(_createAliens())));

Bullet get bullet => new Bullet(new Sprite(_image,120, 0, 1, 10));

Magazin get magazin => _magazin ?? ( _magazin = new Magazin());

Cities get cities => _cities ?? ( _cities = new Cities(new UnmodifiableListView( _createCities())));

// - private ----------------------------------------------------------------------------------

/// Creates the [Alien]-List
List<Alien> _createAliens() {
final List<Alien> aliens = new List<Alien>();

// Alien-Rows in the swarm (see: create())
// pink (_alienSprites index 1)
// blue (_alienSprites index 0)
// blue (_alienSprites index 0)
// light-blue (_alienSprites index 2)
// light-blue (_alienSprites index 2)
final List<int> spriteIndex = [1,0,0,2,2];

if(Swarm.ROWS != spriteIndex.length) {
throw new ArgumentError("Number of Swarm-Rows must be ${spriteIndex.length} but was ${Swarm.ROWS}");
}
for(int rows = 0;rows < Swarm.ROWS;rows++) {

final ToggleSprite sprite = _alienSprites[spriteIndex[rows]];

for(int cols = 0; cols < Swarm.COLS; cols++) {
aliens.add(new Alien(new ToggleSprite(sprite._sprites[0],sprite._sprites[1])));
}
}

return aliens;
}

/// Creates [Cities]
List<City> _createCities() {
List<City> cities = new List<City>();
[1,2,3,4].forEach((_) {
cities.add(new City(new Sprite(_image,84, 8, 36, 24)));
});
return cities;
}
}
Oops, something went wrong.

0 comments on commit 7173958

Please sign in to comment.