aboutsummaryrefslogtreecommitdiff
path: root/game_state.wren
blob: e3208b794ed45d9912f90aacc8a590775657443f (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import "audio" for AudioEngine
import "dome" for Window
import "screens/menu_screen" for MenuScreen
import "screens/credit_screen" for CreditScreen
import "levels/level1" for Level1
import "levels/level2" for Level2
class GameState {
    construct new() {
        AudioEngine.load("menu_music","assets/musics/04 All of Us - Menu.ogg")
        AudioEngine.load("level1_bg","assets/musics/01 A Night Of Dizzy Spells.ogg")
        AudioEngine.load("select","assets/sfx/select.wav")
        AudioEngine.load("credit","assets/musics/Juhani Junkala [Retro Game Music Pack] Ending.wav")
        _fullScreen = false
        _entities = []
        _collidables = []
        setupStates()
    }

    setupStates() {
        __gameStates = {
            "menu": MenuScreen,
            "credit": CreditScreen,
            "level1": Level1,
            "level2": Level2
        }
    }

    toggleFullscreen() {
        _fullScreen = !_fullScreen
        Window.fullscreen = _fullScreen
    }

    switch(level) {
        AudioEngine.stopAllChannels()
        _entities.clear()
        _collidables.clear()
        _currentLevel = __gameStates[level].new(this)
        System.print("Current Level : %(__gameStates[level].name)")
    }

    update() {
        _currentLevel.update()
        for(entity in _entities) {
            entity.update()
        }
    }

    draw(dt) {
        _currentLevel.draw(dt)
        for(entity in _entities) {
            entity.draw(dt)
        }
    }

    addEntities(entity) {
        _entities.add(entity)
    }

    addCollidable(collidable) {
        _collidables.add(collidable)
    }

    checkCollision(entity) {
        var collide = false
        for(collidable in _collidables) {
            System.print(entity.x)
            var entityX = entity.x
            var entityY = entity.y
            if(entityX < (collidable["x"] + collidable["width"]) &&
                (entityX + entity.w) > collidable["x"] &&
                entityY < (collidable["y"] + collidable["height"]) &&
                (entityY + entity.h) > collidable["y"]) {
                collide = true
                break
            }
        }

        return collide
    }
}