<!DOCTYPE html>
<html>
<head>
<title>Angular2 Bee Game</title>
<link data-require="bootstrap-css" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css" />
<link rel="stylesheet" href="style.css" />
<!-- Polyfill(s) for older browsers -->
<script src="https://npmcdn.com/es6-shim@0.35.0/es6-shim.min.js"></script>
<script src="https://npmcdn.com/zone.js@0.6.12?main=browser"></script>
<script src="https://npmcdn.com/reflect-metadata@0.1.3"></script>
<script src="https://npmcdn.com/systemjs@0.19.27/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<body>
<my-app>
loading...
</my-app>
</body>
</html>
.bee-game {
max-width: 1150px;
}
h1 {
font-size: 1.5rem;
color: #999999;
}
h3 {
font-size: 1.15rem;
}
.game-panel {
margin-top: 30px;
}
.pos-1{
display: table;
margin: 0px auto 0px auto;
}
.game-panel ul {
padding: 40px 0px 0px 0px;
}
.game-panel li {
list-style-type: none;
display: inline-block;
margin: 0px 15px 0px 15px;
padding: 0px;
color: #008000;
}
.game-panel .bee-inactive {
color: red;
}
.game-panel .bee-hit {
color: red;
}
.game-panel li div p {
text-align: center;
}
button {
display: block;
height: 28px;
margin: 0px auto;
padding: 0px 30px;
outline: none;
background-color: #f5f5f5;
background-image: -webkit-linear-gradient(center top, whitesmoke, #f1f1f1);
background-image: linear-gradient(center top, whitesmoke, #f1f1f1);
border: 1px solid #dcdcdc;
border-radius: 3px;
}
button:active {
background-color: #a9db80;
background-image: -webkit-linear-gradient(center top, #a9db80, #96c56f);
background-image: linear-gradient(center top, #a9db80, #96c56f);
border: 1px solid #8bcf54;
}
.game-control {
margin-top: 30px;
}
.game-result {
margin-top: 30px;
}
.game-result .result-container {
margin-top: 50px;
}
.game-result .result-container p {
margin: 0px;
text-align: center;
}
.game-result .result-container p span {
font-weight: bold;
}
.game-result h2 {
text-align: center;
color: #008000;
}
.game-result .reset-msg {
color: #999999;
text-align: center;
padding-top: 60px;
}
.game-result button {
margin-top: 30px;
}
@media (min-width: 1200px) {
.game-panel li:first-child {
margin: 0px 10px 0px 0px;
}
.game-panel li:last-child {
margin: 0px 0px 0px 10px;
}
}
### A game using Angular 2 + Typescript + Bootstrap 4
A simple plunker demonstrating Angular2 usage:
- Uses SystemJS + TypeScript to compile on the fly
- Includes binding, directives, http, pipes, and DI usage.
-
### The Bee Game Rules
There is only one **Queen Bee**.
- The Queen Bee has a lifespan of **100 Points**. When hit **8 Points** are deducted from her lifespan
- If the Queen Bee runs out of points, all remaining bees automatically run out of points and the game ends
There are five **Worker Bees**.
- Each Worker Bee has a lifespan of **75 Points**. When hit **10 Points** are deducted from a Worker bee's lifespan
There are eight **Drone Bees**.
- Each Drone Bee has a lifespan of **50 Points**. When hit **12 Points** are deducted from a Drone bee's lifespan
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
//map tells the System loader where to look for things
map: {
app: "./app",
'@angular': 'https://npmcdn.com/@angular',
'rxjs': 'https://npmcdn.com/rxjs@5.0.0-beta.6'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
'@angular/core': {
main: 'core.umd.js',
defaultExtension: 'js'
},
'@angular/compiler': {
main: 'compiler.umd.js',
defaultExtension: 'js'
},
'@angular/common': {
main: 'common.umd.js',
defaultExtension: 'js'
},
'@angular/platform-browser-dynamic': {
main: 'platform-browser-dynamic.umd.js',
defaultExtension: 'js'
},
'@angular/platform-browser': {
main: 'platform-browser.umd.js',
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
}
}
});
import {bootstrap} from '@angular/platform-browser-dynamic';
import {HTTP_PROVIDERS} from '@angular/http';
import './add-rxjs-operators';
import {AppComponent} from './app.component';
bootstrap(AppComponent, [HTTP_PROVIDERS]);
import {Component} from '@angular/core'
import {HTTP_PROVIDERS} from '@angular/http';
import {GameComponent} from './game.component';
import {SwarmService} from './swarm.service';
import {provide} from '@angular/core';
import {XHRBackend} from '@angular/http';
// in-memory web api imports
import {InMemoryBackendService, SEED_DATA } from 'angular2-in-memory-web-api/core';
import {SwarmData} from '../swarm-data';
@Component({
selector: 'my-app',
templateUrl: 'app/app.component.html',
directives: [GameComponent],
providers: [HTTP_PROVIDERS, SwarmService]
})
export class AppComponent {
}
<div class="container-fluid bee-game">
<my-game>TestLoading...</my-game>
</div>
export class Bee {
constructor(
public id: number,
public type: string,
public life_span: number,
public hit_points: number,
public active: boolean
){}
}
{
"data": [
{"id": 1, "type": "queen", "life_span": 100, "hit_points": 8, "active": true},
{"id": 2, "type": "worker", "life_span": 75, "hit_points": 10, "active": true},
{"id": 3, "type": "worker", "life_span": 75, "hit_points": 10, "active": true},
{"id": 4, "type": "worker", "life_span": 75, "hit_points": 10, "active": true},
{"id": 5, "type": "worker", "life_span": 75, "hit_points": 10, "active": true},
{"id": 6, "type": "worker", "life_span": 75, "hit_points": 10, "active": true},
{"id": 7, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 8, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 9, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 10, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 11, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 12, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 13, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 14, "type": "drone", "life_span": 50, "hit_points": 12, "active": true}
]
}
import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';
import {Bee} from './bee';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class SwarmService {
constructor (private http: Http) {}
// private swarmUrl = 'app/swarm.json';
private swarmUrl = 'app/swarm.json';
getSwarm (): Observable<Bee[]> {
return this.http.get(this.swarmUrl)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
private handleError (error: any) {
let errMsg = error.message || error.statusText || 'Server error';
console.error(errMsg);
return Observable.throw(errMsg);
}
}
import {Component, OnInit} from '@angular/core';
import {Bee} from './bee';
import {SwarmService} from './swarm.service';
@Component({
selector: 'my-game',
templateUrl: 'app/game.component.html'
})
export class GameComponent implements OnInit {
title = 'The Bee Game';
swarm: Bee[];
errorMessage: string;
swarmLength: number;
selectedHero: Bee;
pos: number;
isQueenActive: boolean = true;
score: number = 875;
constructor(private swarmService: SwarmService) { };
ngOnInit() {
this.getSwarm();
}
getSwarm() {
this.swarmService.getSwarm().subscribe(
swarm => this.swarm = swarm,
error => this.errorMessage = <any>error
);
}
selectBee() {
this.swarmLength = this.swarm.length;
this.pos = Math.floor(Math.random() * (((this.swarmLength) - 1) - 0 + 1)) + 0;
this.swarm[this.pos].active === true ? this.pos : this.selectBee();
}
hitBee() {
if(this.swarm[this.pos].life_span > this.swarm[this.pos].hit_points) {
this.swarm[this.pos].life_span -= (this.swarm[this.pos].hit_points);
}
else {
this.swarm[this.pos].life_span -= (this.swarm[this.pos].life_span);
this.swarm[this.pos].active = false;
}
}
updateScore() {
if(this.swarm[this.pos].active === false) {
if(this.swarm[this.pos].type === "drone") {
this.score -= 50;
}
else {
if(this.swarm[this.pos].type === "worker") {
this.score -= 75;
}
}
}
}
updateResultView() {
this.isQueenActive = false;
}
reset() {
this.getSwarm();
this.isQueenActive = true;
this.score = 875;
}
play() {
this.selectBee();
this.hitBee();
this.updateScore();
if(this.swarm[0].active === false){
this.updateResultView();
setTimeout(()=>this.reset(), 20000)
}
}
}
<div class="row game-panel">
<div class="col-md-12">
<h1 class="pos-1">{{title}}</h1>
<ul class="pos-1">
<li *ngFor="#bee of swarm" [ngClass]="{'bee-inactive': bee.active === false, 'bee-hit': bee.hit === true}">
<div>
<h3>{{bee.type}}</h3>
<p class="mid">{{bee.life_span}}</p>
</div>
</li>
</ul>
</div>
</div>
<div class="row game-control">
<div class="col-md-12">
<button (click)="play();" [disabled]="isQueenActive === false">Play</button>
</div>
</div>
<div class="row game-result" *ngIf="isQueenActive === false">
<div class="col-md-12">
<div class="result-container">
<h2>Game over!</h2>
<p>You have <span>{{score}}</span> hit points left</p>
<button (click)="reset();">Reset</button>
<p class="reset-msg">The game will reset in 20 seconds</p>
</div>
</div>
</div>
// import 'rxjs/Rx'; // adds ALL RxJS operators to Observable
// Just the Observable operators we need for THIS app.
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
/**
* PLUNKER VERSION (based on systemjs.config.js in angular.io)
* System configuration for Angular 2 samples
* Adjust as necessary for your application needs.
*/
(function(global) {
var ngVer = '@2.0.0-rc.1';
//map tells the System loader where to look for things
var map = {
'app': 'app',
'@angular': 'https://npmcdn.com/@angular', // sufficient if we didn't pin the version
'angular2-in-memory-web-api': 'https://npmcdn.com/angular2-in-memory-web-api', // get latest
'rxjs': 'https://npmcdn.com/rxjs@5.0.0-beta.6',
'ts': 'https://npmcdn.com/plugin-typescript@4.0.10/lib/plugin.js',
'typescript': 'https://npmcdn.com/typescript@1.8.10/lib/typescript.js',
};
//packages tells the System loader how to load when no filename and/or no extension
var packages = {
'app': { main: 'main.ts', defaultExtension: 'ts' },
'rxjs': { defaultExtension: 'js' },
'angular2-in-memory-web-api': { defaultExtension: 'js' },
};
var ngPackageNames = [
'common',
'compiler',
'core',
'http',
'platform-browser',
'platform-browser-dynamic',
'router',
'router-deprecated',
'upgrade',
];
// Add map entries for each angular package
// only because we're pinning the version with `ngVer`.
ngPackageNames.forEach(function(pkgName) {
map['@angular/'+pkgName] = 'https://npmcdn.com/@angular/' + pkgName + ngVer;
});
// Add package entries for angular packages
ngPackageNames.forEach(function(pkgName) {
// Bundled (~40 requests):
packages['@angular/'+pkgName] = { main: pkgName + '.umd.js', defaultExtension: 'js' };
// Individual files (~300 requests):
//packages['@angular/'+pkgName] = { main: 'index.js', defaultExtension: 'js' };
});
var config = {
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
}
},
map: map,
packages: packages
}
System.config(config);
})(this);
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
export class SwarmData {
createDb() {
let swarm = [
{"id": 1, "type": "queen", "life_span": 100, "hit_points": 8, "active": true},
{"id": 2, "type": "worker", "life_span": 75, "hit_points": 10, "active": true},
{"id": 3, "type": "worker", "life_span": 75, "hit_points": 10, "active": true},
{"id": 4, "type": "worker", "life_span": 75, "hit_points": 10, "active": true},
{"id": 5, "type": "worker", "life_span": 75, "hit_points": 10, "active": true},
{"id": 6, "type": "worker", "life_span": 75, "hit_points": 10, "active": true},
{"id": 7, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 8, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 9, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 10, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 11, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 12, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 13, "type": "drone", "life_span": 50, "hit_points": 12, "active": true},
{"id": 14, "type": "drone", "life_span": 50, "hit_points": 12, "active": true}
];
return {swarm};
}
}
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}