aboutsummaryrefslogtreecommitdiff
path: root/src/view/scene.ts
blob: 6d35edc19a4193df08ba8064fcf0f071ee31ec2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import * as Ship from 'view/ship'
import * as Colors from 'view/colors'
import * as Vec2 from 'model/vec2'
import * as Size from 'model/size'

export interface State {
  canvas: HTMLCanvasElement,
  context: CanvasRenderingContext2D,
  timestamp: number,
  ship: Ship.State,
  windowSize: Size.Size,
}

export function init(): State {
  const canvas = document.querySelector('canvas') as HTMLCanvasElement
  const context = canvas.getContext("2d") as CanvasRenderingContext2D
  const windowSize = {
    width: window.innerWidth,
    height: window.innerHeight
  }
  canvas.width = windowSize.width
  canvas.height = windowSize.height

  return {
    canvas,
    context,
    timestamp: 0,
    ship: Ship.init(windowSize),
    windowSize
  }
}

export function update(state: State, timestamp: number) {
  const dt = timestamp - state.timestamp
  state.timestamp = timestamp

  Ship.update(state.ship, dt, state.windowSize)
}

export function resize(state: State, size: Size.Size) {
  Ship.project(state.ship, state.windowSize, size)
  state.windowSize = size
  state.canvas.width = size.width
  state.canvas.height = size.height
}

export function view(state: State) {
  // Clear
  state.context.fillStyle = Colors.colors.blue
  state.context.fillRect(0, 0, state.windowSize.width, state.windowSize.height)

  Ship.view(state.context, state.ship)
}