import * as React from 'react'; import Sky from './Sky'; import Ground from './Ground'; import CannonPipe from './CannonPipe'; import CannonBase from './CannonBase'; import { Store } from '../store'; import { getCanvasPosition, Point, handleResize } from '../utils/formulas'; import { inject, observer } from 'mobx-react'; import CannonBall from './CannonBall'; import CurrentScore from './CurrentScore'; import FlyingObject from './FlyingObject'; import Heart from './Heart'; import StartGame from './StartGame'; import Title from './Title'; import { gameHeight, gameWidth } from '../utils/constants'; interface CanvasProps { store?: Store; } // Needed to compile feDropShadow interface SVGFEDropShadowElement extends HTMLElement { } declare global { namespace JSX { interface IntrinsicElements { feDropShadow: React.SVGProps; } } } @inject('store') @observer export class Canvas extends React.Component { private mousePosition: Point = { x: 0, y: 0 }; componentDidMount() { setInterval(() => { if (this.props.store) { this.props.store.setAngle(this.mousePosition); if (this.props.store.started) { this.props.store.createFlyingObject(); this.props.store.moveCannonBalls(); this.props.store.checkCollisions(); } } }, 20); window.onresize = handleResize; window.onresize(new UIEvent('resize')); } trackMouse(event: React.MouseEvent) { this.mousePosition = getCanvasPosition(event); } shoot(event: React.MouseEvent) { if (this.props.store && this.props.store.started) { this.props.store.shoot(getCanvasPosition(event)); } } render() { const viewBox = [ gameWidth / -2, 100 - gameHeight, gameWidth, gameHeight ]; const style : React.CSSProperties = { width: "100%", margin: "0" } return ( this.trackMouse(event))} onClick={(event => this.shoot(event))} style={style} > { (!this.props.store || !this.props.store.started) && } { this.props.store && this.props.store.started && { this.props.store.flyingObjects.map(flyingObject => ( )) } { this.props.store.cannonBalls.map(cannonBall => ( )) } } ); } }