aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/WorldScene.java
blob: 99ac2918bf697c95b25751493d70a0cdd9bd1f17 (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
81
82
83
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import java.util.ArrayList;



public class WorldScene extends Scene {
    
    private ArrayList<Actor> actors = new ArrayList<Actor>();
    private Map map;

    private int tileSize;

    public WorldScene(Dimension gridSize, int tileSize) {
        this.tileSize = tileSize;
        map = new Map(gridSize);


        // TODO remove hardcoded stuff 
        Player player = new Player("pipo", gridSize);
        this.actors.add(player);
    }

    @Override
    public void render(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        // draw tiles
        for (Tile tile : this.map.grid) {
            switch (tile.type) {
                case GRASS:
                    g2d.setColor(Palette.GREEN);
                    break;
                case WATER:
                    g2d.setColor(Palette.BLUE);
                    break;
            }

            g2d.fillRect(
                this.tileSize * tile.x,
                this.tileSize * tile.y,
                this.tileSize, this.tileSize
            );

            // draw grid
            // composite (enable alpha)
            Composite originalComposite = g2d.getComposite();
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f));

            g2d.setPaint(Color.BLACK);
            g2d.drawRect(
                this.tileSize * tile.x,
                this.tileSize * tile.y,
                this.tileSize, this.tileSize
            );

            g2d.setComposite(originalComposite);
        }

        // draw actors
        for (Actor actor : this.actors) {
            switch (actor.type) {
                case PLAYER:
                    g2d.setColor(Palette.ORANGE);
                    break;
                case ENEMY:
                    g2d.setColor(Palette.RED);
                    break;
            }
            int gap = this.tileSize/10;
            g2d.fillRect(
                (this.tileSize * actor.x) + gap,
                (this.tileSize * actor.y) + gap,
                this.tileSize - gap*2, this.tileSize - gap*2
            );
        }
    }
}