Warning: file_get_contents(http://s4p.us/a/api/?method=generateUrl&param=http%3A%2F%2Fpaste4.us%2Fview_paste%2F24%2F) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in /home/content/82/8776282/html/evbpc.com/paste_system/code/view_paste.php on line 21
Play
Fullscreen Options: With Syntax Highlighting | Plain Text | Design View
Short Link:
Play
By Aaron
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
 package com.slickgame.states;

import javax.swing.JOptionPane;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

import com.karmatree.slickgame.Game;
import com.karmatree.slickgame.Globals;
import com.karmatree.slickgame.components.Button;
import com.karmatree.slickgame.entity.Player;

public class Play extends BasicGameState {
    static TiledMap level;
    static Rectangle[][] tileRect;
    
    Image gui;
    Image portrait;
    
    Image health;
    Image mana;
    Image exp;
    
    //buttons
    Button inventory;
    Button equipment;
    Button life;
    Button quests;
    Button guide;
    Button options;
    
    public Play(int play) {
        
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        //player card
        gui = new Image("res/gfx/gui/card.png");
        portrait = new Image("res/gfx/portraits/player/wizard.png");
        
        //vitals
        health = new Image("res/gfx/player/health_full.png");
        mana = new Image("res/gfx/player/mana_full.png");
        exp = new Image("res/gfx/player/exp_full.png");
        
        //buttons
        inventory = new Button(55355"inv""inv_hover", gc);
        equipment = new Button(90355"com""com_hover", gc);
        life = new Button(125355"life""life_hover", gc);
        quests = new Button(160355"quests""quests_hover", gc);
        guide = new Button(195355"guide""guide_hover", gc);
        options = new Button(230355"spell""spell_hover", gc);
        
        //fonts
        Globals.calibri15 = new AngelCodeFont("res/fonts/calibri15.fnt""res/fonts/calibri15.png");

        Globals.player = new Player();
        
        level = new TiledMap("res/maps/level.tmx");
                
        tileRect = new Rectangle[level.getWidth()][level.getHeight()];
        boolean[][] blocked = new boolean[level.getWidth()][level.getHeight()];
        
        for(int x=0;x<level.getWidth(); x++) {
            for (int y=0;y<level.getHeight(); y++) {
                int tileID = level.getTileId(x, y, 1);
                
                // check if blocked
                String value = level.getTileProperty(tileID, "blocked""false");
                if ("true".equals(value)) blocked[x][y] = true;
                        
                // tile rect
                if(blocked[x][y]) {
                    tileRect[x][y] = new Rectangle(x*Globals.TILE_SIZE, y*Globals.TILE_SIZE, Globals.TILE_SIZE, Globals.TILE_SIZE);
                } else {
                    tileRect[x][y] = new Rectangle(0,0, 0,0);
                }
            } 
        } 
    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        g.setBackground(Color.black);
        level.render(Globals.camX(0,gc), Globals.camY(0,gc));
        
        Globals.player.render(gc);
        
        //player card
        g.drawImage(gui, 10395);
        g.drawImage(portrait, 20412);
        
        g.drawImage(health, 65412);
        g.drawImage(mana, 65427);
        g.drawImage(exp, 65442);
        
        Globals.calibri15.drawString(180408"Aaron");
        Globals.calibri15.drawString(180424"Level " + Player.level);
        Globals.calibri15.drawString(180441"Gold: " + Player.gold);
        
        //FPS
        //Globals.calibri15.drawString(1010"FPS: " + gc.getFPS());
        
        //icons    
        inventory.render(gc, g);
        equipment.render(gc, g);
        life.render(gc, g);
        quests.render(gc, g);
        guide.render(gc, g);
        options.render(gc, g);
    }

    int tmrMovement;
    final int SPEED = 3;
    
    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
        Input input = gc.getInput();
        
        tmrMovement += delta;
        if(tmrMovement >= 10) {
            checkMovement(input, delta);
            tmrMovement = 0;
        }
        
        if(input.isKeyDown(Input.KEY_ESCAPE)) {
            System.exit(0);
        }
        
        buttonClick(gc, delta);
        
        //level testing
        if(input.isKeyPressed(Input.KEY_L)) {
            Player.level += 1;
        }
        
        // make sure max level is max
        if(Player.level > Globals.max_level) {
            Player.level = Globals.max_level;
        }
    }
    
    public void checkMovement(Input input, int delta) {
        if(input.isKeyDown(Input.KEY_W) || input.isKeyDown(Input.KEY_A) || input.isKeyDown(Input.KEY_S) || input.isKeyDown(Input.KEY_D)) {
            if(Globals.player.moving == false) {
                Globals.player.moving = true;
                Globals.player.anim[Globals.player.dir].restart();
            }
        }
        
        // walking
        if(input.isKeyDown(Input.KEY_A)) {
            Globals.player.dir = Globals.DIR_LEFT;
            if(!isBlocked(Globals.player.x-SPEED, Globals.player.y, Globals.DIR_LEFT)) {
                Globals.player.setPos(Globals.player.x-SPEED, Globals.player.y);
            }
        } else if(input.isKeyDown(Input.KEY_D)) {
            Globals.player.dir = Globals.DIR_RIGHT;
            if(!isBlocked(Globals.player.x+SPEED, Globals.player.y, Globals.DIR_RIGHT)) {
                Globals.player.setPos(Globals.player.x + SPEED, Globals.player.y);
            }
        } else if(input.isKeyDown(Input.KEY_W)) {
            Globals.player.dir = Globals.DIR_UP;
            if(!isBlocked(Globals.player.x, Globals.player.y-SPEED, Globals.DIR_UP)) {
                Globals.player.setPos(Globals.player.x, Globals.player.y - SPEED);
            }
        } else if(input.isKeyDown(Input.KEY_S)) {
            Globals.player.dir = Globals.DIR_DOWN;
            if(!isBlocked(Globals.player.x, Globals.player.y+SPEED, Globals.DIR_DOWN)) {
                Globals.player.setPos(Globals.player.x, Globals.player.y + SPEED);
            }
        } else {
            Globals.player.moving = false;
            Globals.player.anim[Globals.player.dir].stop();
        }
        
        // sync animations
        Globals.player.anim[Globals.player.dir].update(delta);
    }
    
    public boolean isBlocked(int sx, int sy, int dir) {
        // find the tile id
        int x = sx / Globals.TILE_SIZE;
        int y = sy / Globals.TILE_SIZE;
        
        // don't go out of bounds
        if(x<0 || x == level.getWidth() || y<0 || y == level.getHeight()) return true;

        // create the player rectangle
        Rectangle playerRect = new Rectangle(sx, sy, Globals.PLAYER_W, Globals.PLAYER_H);
        
        // check for intersections
        for(int i=0; i<level.getWidth(); i++) {
            for(int j=0; j< level.getHeight(); j++) {
                if(playerRect.intersects(tileRect[i][j]) && tileRect[i][j].getWidth()>0) {
                    return true;
                }
            }
         }
        
        return false;
    }
    
    private void buttonClick(GameContainer gc, int delta) {
        Input input = gc.getInput();
        if(inventory.isOver && input.isMouseButtonDown(0)) {
            JOptionPane.showMessageDialog(null"Inventory not implemented.", Game.gamename, 1);
        }
        
        if(equipment.isOver && input.isMouseButtonDown(0)) {
            JOptionPane.showMessageDialog(null"equipment!!!", Game.gamename, 1);
        }
        
        if(life.isOver && input.isMouseButtonDown(0)) {
            JOptionPane.showMessageDialog(null"Player stats maybe?", Game.gamename, 1);
        }
        
        if(quests.isOver && input.isMouseButtonDown(0)) {
            JOptionPane.showMessageDialog(null"Make sure to switch this to a quest State", Game.gamename, 1);
        }
        
        if(guide.isOver && input.isMouseButtonDown(0)) {
            JOptionPane.showMessageDialog(null"Some guide will be opened!", Game.gamename, 1);
        }
        
        if(options.isOver && input.isMouseButtonDown(0)) {
            JOptionPane.showMessageDialog(null"Options yo!", Game.gamename, 1);
        }
    }
    
    @Override
    public int getID() {
        return Game.play;
    }