Skip to content

Commit eb3d431

Browse files
committed
implement connector and puck logic
1 parent 382825f commit eb3d431

File tree

4 files changed

+178
-5
lines changed

4 files changed

+178
-5
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import mojs from 'mo-js';
2+
3+
/**
4+
* Connector between Junctions
5+
*/
6+
export default class Connector {
7+
8+
/**
9+
* Uses as a mute for the rescale process
10+
*/
11+
const MUTE = 100;
12+
const CLASS_NAME = "connector";
13+
const SHAPE = "rect";
14+
const STROKE = 0;
15+
const POINTS = 0;
16+
17+
/**
18+
* Coordinates
19+
*/
20+
let x = 0;
21+
let y = 0;
22+
let top: string; // % value
23+
let left: string; // % value
24+
25+
let length: number;
26+
let rotation: number;
27+
28+
/**
29+
* @param x coordinate
30+
* @param y coordinate
31+
* @param length
32+
* @param rotation
33+
*/
34+
constructor(x: number, y: number, length: number, rotation: number) {
35+
this.x = x;
36+
this.y = y;
37+
this.length = length;
38+
this.rotation = rotation;
39+
40+
this.left = this.calculateOffset(x);
41+
this.top = this.calculateOffset(y);
42+
43+
this.mojs = this.generateMojs();
44+
}
45+
46+
/**
47+
* @Override
48+
* Creates a new mojs from class variables
49+
* @returns {mojs.Shape}
50+
*/
51+
private generateMojs() {
52+
return new mojs.Shape({
53+
parent: "#" + Game.defaults.BOARD_ID,
54+
className: this.CLASS_NAME,
55+
shape: this.SHAPE,
56+
points: this.POINTS,
57+
strokeWidth: this.STROKE,
58+
radius: this.radius(),
59+
left: this.left + "%",
60+
top: this.top + "%",
61+
62+
angle: this.rotation,
63+
radiusY: this.calculateScale(),
64+
isShowStart: true
65+
});
66+
};
67+
68+
private radius() {
69+
return this.length * document.getElementById(Game.defaults.BOARD_ID).offsetWidth / this.MUTE_RADIUS;
70+
}
71+
72+
/**
73+
* @Override
74+
* Rescales the mojs object
75+
*/
76+
rescale() {
77+
this.mojs.tune({
78+
radius: this.radius(),
79+
radiusY: this.calculateScale()
80+
})
81+
.replay();
82+
}
83+
84+
}

app/angular2/src/app/play/junction.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import mojs from 'mo-js';
2+
13
/**
24
* Junction
35
* Holds the mojs shape
@@ -18,13 +20,13 @@ export default Junction {
1820
*/
1921
let x = 0;
2022
let y = 0;
21-
let top: String; // % value
22-
let left: String; // % value
23+
let top: string; // % value
24+
let left: string; // % value
2325

2426
/**
2527
* HTML ID
2628
*/
27-
let id: String;
29+
let id: string;
2830

2931
/**
3032
* @param x coordinate

app/angular2/src/app/play/play.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22
import Junction from './Junction';
3+
import Connector from './Connector';
4+
import Puck from './Puck';
35

46
@Component({
57
selector: 'app-play',
@@ -11,8 +13,10 @@ import Junction from './Junction';
1113
*/
1214
export class PlayComponent implements OnInit {
1315

14-
constructor() { }
15-
16+
constructor() {
17+
18+
}
19+
1620
ngOnInit() {
1721
}
1822

app/angular2/src/app/play/puck.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import mojs from 'mo-js';
2+
3+
/**
4+
* Puck thats placed on a Junction
5+
* Holds the mojs shape
6+
* @param x coordinate
7+
* @param y coordinate
8+
* @param id
9+
* @constructor
10+
*/
11+
export default class Puck {
12+
13+
/**
14+
* Uses as a mute for the rescale process
15+
*/
16+
const MUTE = 25;
17+
const CLASS_NAME = "puck hidden";
18+
const CLASS_NAME_ACTIVATED = "puck";
19+
const SHAPE = "polygon";
20+
const STROKE = 3;
21+
const POINTS = 6;
22+
23+
/**
24+
* Coordinates
25+
*/
26+
let x = 0;
27+
let y = 0;
28+
let top: string; // % value
29+
let left: string; // % value
30+
31+
let length: number;
32+
let rotation: number;
33+
34+
constructor(x: number, y: number, id: string) {
35+
this.x = x;
36+
this.y = y;
37+
this.id = id;
38+
39+
this.left = this.calculateOffset(x);
40+
this.top = this.calculateOffset(y);
41+
42+
this.mojs = this.generateMojs();
43+
this.mojs.el.dataset.id = id;
44+
}
45+
46+
/**
47+
* Activates puck on the board
48+
* @param man player of puck
49+
*/
50+
activate(man) {
51+
$(this.mojs.el).attr("class", this.CLASS_NAME_ACTIVATED + " " + man.toLowerCase());
52+
this.mojs.play();
53+
}
54+
55+
/**
56+
* Deactivates and hide the puck on the board
57+
*/
58+
deactivate() {
59+
$(this.mojs.el).attr("class", this.CLASS_NAME);
60+
}
61+
62+
/**
63+
* Puck Event Handler
64+
* !!@this is here the clicked element!!
65+
*/
66+
clickEvent() {
67+
var data = Game.State.data,
68+
playerState = data.currentPlayer.currentState === "HOP" ? "MOVE": data.currentPlayer.currentState;
69+
70+
// prevent player from selection opponents puck
71+
if (playerState !== "PICK" && !$(this).hasClass(data.currentPlayer.man.toLowerCase())) {
72+
return;
73+
}
74+
if (playerState === "MOVE" && Game.mouseQueue.length === 0) {
75+
Game.mouseQueue.push(this);
76+
$(this).addClass("selected");
77+
return;
78+
}
79+
//Game.State.requestCommand(playerState.toLowerCase(), [this.dataset.id]);
80+
Game.Socket.send("processCommand", playerState.toLowerCase(), [this.dataset.id]);
81+
}
82+
83+
}

0 commit comments

Comments
 (0)