|
Resetting Player Location on State Change |
Lasaqus7
Member #15,950
May 2015
|
Back again :-(. What I am trying to do is when my state changes to COMBAT, I need my players location to be reset to another location rather than the last location he was on the map. I did actually have this working before (until I deleted the code to fix another issue) now I can't seem to figure it out again. 1
2enum STATE{MENU, PLAYING, COMBATSCREEN, COMBAT, GAMEOVER};
3
4Player Human;
5
6//Prototypes
7void ChangeState(int &state, int newState);
8
9void InitHuman(Player &Human, ALLEGRO_BITMAP *image);
10void DrawHuman(Player &Human);
11void MoveHumanLeft(Player &Human);
12void MoveHumanRight(Player &Human);
13
14void DrawCombatHuman(Player &Human);
15
16int main(void)
17{
18 //ALLEGRO VARIABLES
19 ALLEGRO_DISPLAY *display = NULL;
20 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
21 ALLEGRO_TIMER *timer = NULL;
22 ALLEGRO_BITMAP *HumanImage;
23
24 //ALLEGRO INIT FUNCTIONS
25 if (!al_init()) //Initialize Allegro
26 return -1;
27
28 display = al_create_display(WIDTH, HEIGHT); //Create display object
29
30 if (!display) //Test display object
31 return -1;
32
33 //ADDON INSTALL
34 al_init_primitives_addon();
35 al_install_keyboard();
36 al_install_mouse();
37 al_init_image_addon();
38 al_init_font_addon();
39 al_init_ttf_addon();
40
41 //PROJECT INIT
42 event_queue = al_create_event_queue();
43 timer = al_create_timer(1.0 / FPS);
44
45 //LOAD IMAGES AND SPRITESHEETS
46 HumanImage = al_load_bitmap("spritesheethuman.bmp");
47 al_convert_mask_to_alpha(HumanImage, al_map_rgb(106, 76, 48));
48
49 srand(time(NULL));
50 //INIT CHARACTERS
51 ChangeState(state, MENU);
52
53 InitHuman(Human, HumanImage);
54//InitCombatHuman(Human, HumanImage); TESTING
55
56 //TIMER INIT AND STARTUP
57 al_register_event_source(event_queue, al_get_keyboard_event_source());
58 al_register_event_source(event_queue, al_get_mouse_event_source());
59 al_register_event_source(event_queue, al_get_timer_event_source(timer));
60 al_register_event_source(event_queue, al_get_display_event_source(display));
61
62 al_start_timer(timer);
63 while (!done)
64 {
65 ALLEGRO_EVENT ev;
66 al_wait_for_event(event_queue, &ev);
67
68 if (ev.type == ALLEGRO_EVENT_TIMER)
69 {
70 render = true;
71
72 //UPDATE===========================================
73 if (keys[UP] || keys[W])
74 MoveHumanUp(Human);
75 else if (keys[DOWN] || keys[S])
76 MoveHumanDown(Human);
77 else if (keys[LEFT] || keys[A])
78 MoveHumanLeft(Human);
79 else if (keys[RIGHT] || keys[D])
80 MoveHumanRight(Human);
81 else if (keys[SPACE])
82 //=====================================================
83 if (state == MENU)
84 {
85
86 }
87 else if (state == PLAYING)
88 {
89
90 }
91 else if (state == COMBATSCREEN)
92 {
93
94 }
95 else if (state == COMBAT)
96 {
97
98 }
99 }
100
101 //==========================================================
102 //RENDER
103 //==========================================================
104 if (render && al_is_event_queue_empty(event_queue))
105 {
106 render = false;
107
108 if (state == MENU)
109 {
110 }
111 else if (state == PLAYING)
112 {
113 DrawHuman(Human);
114 }
115 else if (state == COMBATSCREEN)
116 {
117 if (keys[SPACE])
118 state = COMBAT;
119 }
120 else if (state == COMBAT)
121 {
122 //InitCombatHuman(Human); If I put this here, players starts at the right location but cannot move.
123 DrawCombatHuman(Human);
124 }
125 else if (state == GAMEOVER)
126 {
127
128 }
129
130 //FLIP BUFFERS =========================================
131 al_flip_display();
132 al_clear_to_color(al_map_rgb(0, 0, 0));
133 }
134 }
135
136 //==========================================================
137 //DESTROY PROJECT OBJECTS
138 //==========================================================
139
140 al_destroy_bitmap(HumanImage);
141 al_destroy_event_queue(event_queue);
142 al_destroy_timer(timer);
143 al_destroy_font(font18);
144 al_destroy_display(display);
145
146 return 0;
147}
148
149void InitHuman(Player &Human, ALLEGRO_BITMAP *image = NULL)
150{
151 Human.x = 50;
152 Human.y = 460;
153 Human.ID = PLAYER;
154 Human.speed = 2;
155 Human.boundx = 15;
156 Human.boundy = 25;
157
158 Human.maxFrame = 8;
159 Human.curFrame = 0;
160 Human.frameCount = 0;
161 Human.frameDelay = 5;
162 Human.frameWidth = 96;
163 Human.frameHeight = 96;
164 Human.animationColumns = 13;
165 Human.animationDirection = 1;
166
167 Human.animationRow = 0;
168
169 if (image != NULL)
170 Human.image = image;
171}
172//void InitCombatHuman(Player &Human, ALLEGRO_BITMAP *image = NULL)
173//{
174//Human.x = 50; TESTING
175//Human.y = 460;
176//}
177void DrawHuman(Player &Human)
178{
179 int fx = (Human.curFrame % Human.animationColumns) * Human.frameWidth;
180 int fy = Human.animationRow * Human.frameHeight;
181
182 al_draw_bitmap_region(Human.image, fx, fy, Human.frameWidth, Human.frameHeight, Human.x - Human.frameWidth / 2, Human.y - Human.frameHeight / 2, 0);
183}
184void DrawCombatHuman(Player &Human)
185{
186 Human.x = 50; //If i add these x/y my player is located at the right place but unable to move
187 Human.y = 460; // If I remove them my player is located at the last location he was in the previous state
188
189 int fx = (Human.curFrame % Human.animationColumns) * Human.frameWidth;
190 int fy = Human.animationRow * Human.frameHeight;
191
192 al_draw_bitmap_region(Human.image, fx, fy, Human.frameWidth, Human.frameHeight, Human.x - Human.frameWidth / 2, Human.y - Human.frameHeight / 2, 0);
193}
194
195void MoveHumanLeft(Player &Human)
196{
197 Human.animationRow = 4;
198 Human.animationColumns = 8;
199 Human.animationDirection = 1;
200 if (++Human.frameCount >= Human.frameDelay)
201 {
202 Human.curFrame += Human.animationDirection;
203 if (Human.curFrame >= Human.maxFrame)
204 Human.curFrame = 0;
205 else if (Human.curFrame <= 0)
206 Human.curFrame = Human.maxFrame - 1;
207
208 Human.frameCount = 0;
209 }
210
211 Human.x -= Human.speed;
212 if (Human.x < 0)
213 Human.x = 0;
214}
215void MoveHumanRight(Player &Human)
216{
217 Human.animationRow = 2;
218 Human.animationColumns = 8;
219 Human.animationDirection = 1;
220 if (++Human.frameCount >= Human.frameDelay)
221 {
222 Human.curFrame += Human.animationDirection;
223 if (Human.curFrame >= Human.maxFrame)
224 Human.curFrame = 0;
225 else if (Human.curFrame <= 0)
226 Human.curFrame = Human.maxFrame - 1;
227
228 Human.frameCount = 0;
229 }
230 Human.x += Human.speed;
231 if (Human.x > WIDTH)
232 Human.x = WIDTH;
233}
234
235void ChangeState(int &state, int newState)
236{
237 if (state == MENU)
238 {
239 }
240 else if (state == PLAYING)
241 {
242 }
243 else if (state == COMBATSCREEN)
244 {
245
246 }
247 else if (state == COMBAT)
248 {
249 }
250
251 state = newState;
252
253 if (state == MENU)
254 {
255 }
256 else if (state == PLAYING)
257 {
258 InitHuman(Human);
259 InitEnemy(Orc);
260 }
261 else if (state == COMBATSCREEN)
262 {
263 //InitCombatHuman <-- Tried to declare different x/y values here
264 }
265 else if (state == COMBAT)
266 {
267 }
268}
I have tried to add in a new function InitCombatHuman so I could declare different x/y values for when the state == COMBAT although he still started in the location he was in the PLAYING state. I tried to delete all unnecessary code so it maybe easier to see the issue. EDIT :- It's fine now I think, so far I managed to get the player to relocate correctly and move. |
beoran
Member #12,636
March 2011
|
Lasaqus7 said: I did actually have this working before (until I deleted the code to fix another issue) now I can't seem to figure it out again. You are in dire need of learning how to use a version control system or VCS. Why not go pro and learn how to use git (http://git-scm.com/)? With a VCS you can make a commit when something works and if you break it later you can go back in time and see what you have changed and even undo some changes without losing your new work. Knowledge of using a vcs is an essential programmer skill IMHO. |
Lasaqus7
Member #15,950
May 2015
|
Cheers beoran, I'll have to check something like that out and if something works, not to delete it just comment it out. |
Chris Katko
Member #1,881
January 2002
|
Or at the very least, at the end of the day, copy/zip the folder containing your code and game, and keep each version. -----sig: |
beoran
Member #12,636
March 2011
|
Well that's the advantage of using a VCS properly. As long as you commit often, you don't need to keep tons of zip files or backup folders around. And you can actually delete code without worries as long as you make a commit before doing it. A VCS allows you to restore the sources to any version you previously made a commit of. What Chris proposes can also work, but making daily backups is really a poor man's "VCS". A real VCS like Git, or mercurial is much more useful. |
l j
Member #10,584
January 2009
|
If you're using VS2013, it has a easy to use git plugin built-in (Well I guess it's not really a plugin anymore then, but aside from that).
|
Lasaqus7
Member #15,950
May 2015
|
I'll definitely be looking at it and I am using VS2013. I like it how you ask a question, and even though I fixed my issue, I still get help and advice on ways to improve my working |
|