Home>戻る

〜第七章 一定距離ずつ移動〜


今回はボールを一定距離ずつ動かしてみましょう。
RPG特有の移動の仕方ですが、 これによって階段で移動したり人と会話する時など、いろいろな判定がしやすくなります。

今まではボールの座標をドット単位の座標で指定してきましたが、今回からマップ上での座標も指定するようにします。
マップチップは32ドットの大きさなので、ボールが(96,32)の位置にあるなら、32で割って(3,1)の位置となります。

そして、マップチップとうまく重なるように一定距離ずつ移動させてやる必要があります。
ボールが(100,43)なんて中途半端な位置で止まってはいけません。
これは一定距離移動したあと、移動後のボールの位置=マップでの座標×32ドット、というように指定してやります。
これによってピッタリ移動することが出来ます。

今回加えた外部変数はこちらです。
int ball_x,ball_y;//マップ上でのボール位置
boolean ball_moving;//ボールは移動中か(yes=true)
int ball_moving_dir;//移動中の方向(上=0,下=1,左=2,右=3)
float sum_move_length;//移動距離(32ドットずつ)
移動中はフラグ(ball_moving)をセットし、他のキー入力を受け付けないようにします。
移動完了後に、フラグを解除してやります。

ではプログラムの公開です。

Rpg07.java
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// RPG07 第七章 一定距離ずつ移動
// 2004/07/12

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.URL;

public class Rpg07 extends Applet implements Runnable,KeyListener{
   Image buffer;//イメージバッファ
   Graphics bufferg;//バックバッファ
   
   final int CHIP_SIZE=32;//チップサイズ

   float FrameTime;//1フレームあたりの時間(秒)
   long waitTime;//現在時刻保存用

   Image ball_image;//ボールイメージ
   float ball_px,ball_py;//ボール座標
   int bl_x,ball_y;//マップ上でのボール位置

   float ball_speed;//ボールの速度
   boolean ball_moving;//ボールは移動中か(yes=true)
   int ball_moving_dir;//移動中の方向(上=0,下=1,左=2,右=3)
   float sum_move_length;//移動距離(32ドットずつ)

   //キーの状態(離している=0、押し続けている=1、押した瞬間=2)
   int key_states[] = new int[5];//4方向(上=0,下=1,左=2,右=3)+Zキー=4

   //マップデータ
   int map_data[][] = new int[64][64];//64×64
   int map_width,map_height;//マップの横幅、高さ(チップ数)
   Image map_image;//マップチップ画像
   
   //セルテーブル
   MapCell celltbl[] = {
      new MapCell(0,0,false),//草原
      new MapCell(1,0,true),//芝生A
      new MapCell(2,0,true),//芝生B
      new MapCell(3,0,false),//家
      new MapCell(4,0,false),//木A

      new MapCell(5,0,true),//木B
      new MapCell(6,1,true),//城
      new MapCell(7,2,true),//海
      new MapCell(8,3,true),//塔の上
      new MapCell(9,4,true),//塔の下

      new MapCell(10,0,true),//洞窟
      new MapCell(11,0,true),//岩山
      new MapCell(12,5,true),//宇宙A
      new MapCell(13,5,true),//宇宙B
      new MapCell(14,5,true),//クリスタル
   };

   //初期化
   public void init(){
      Thread t;
      Dimension d = getSize();
      MediaTracker mediaT = new MediaTracker(this);

      //ボール画像ロード
      ball_image = getImage(getDocumentBase(),"chara13.gif");
      mediaT.addImage(ball_image,0);
      
      //マップチップ画像ロード
      map_image = getImage(getDocumentBase(),"map_chip.gif");
      mediaT.addImage(map_image,0);

      //ロード待ち
      try{
         mediaT.waitForAll();
      }catch(InterruptedException e){}

      //マップデータロード(20×15サイズ)
      readData("map00.dat",20,15);

      ball_speed = 128;//ボールの速度。1秒で128ドット。

      //ボールの初期座標(マップ上で4,6の位置)
      ball_x = 4;
      ball_y = 6;
      ball_px = ball_x*CHIP_SIZE;
      ball_py = ball_y*CHIP_SIZE;

      //現在時刻保存
      waitTime=System.currentTimeMillis();

      addKeyListener(this);//キー入力リスナー
      requestFocus();//フォーカス取得

      //スレッドを開始する
      t = new Thread(this);
      t.start();

      //バッファを作成する
      buffer = createImage(d.width,d.height);
   }


   //マップデータを読み込む
   private void readData(String filename,int max_x,int max_y){
      int i,x,y;
      InputStream is = null;

      map_width = max_x;
      map_height = max_y;

      try{
         is = new URL(getDocumentBase(),filename).openStream();

         x=y=0;
         while((i=is.read()) != -1){
            map_data[x][y]=i;
            if(++x == map_width){
               ++y;
               x=0;

               if(y==map_width)break; //終了条件
            }
         }
         is.close();
      }catch(IOException e){}
   }

   //実行
   public void run(){
      try{
         //ここがゲームループ
         while(true){
            //1フレーム時間計算(秒)
            FrameTime = ( System.currentTimeMillis()-waitTime ) /1000.0f;
            waitTime = System.currentTimeMillis();

            action();//処理
            repaint();//描画

            //ちょっと休憩(0.01秒)
            Thread.sleep(10);
         }
      }
      catch(Exception e){
      }
   }
   
   //描画更新
   public void update(Graphics g){
      paint(g);
   }

   //描画
   public void paint(Graphics g){
      int i,j,x,y;
      int cell_data,gra;

      //バッファのグラフィックコンテキストを取得する
      if(bufferg == null){
         bufferg = buffer.getGraphics();
      }
      
      //バッファを描画する
      Dimension d = getSize();
      bufferg.setColor(Color.white);
      bufferg.fillRect(0,0,d.width,d.height);

      //---------------------マップ描画------------------------

      //キャラ座標を取得
      int view_x = (int)ball_px + CHIP_SIZE/2;
      int view_y = (int)ball_py + CHIP_SIZE/2;
      
      //カメラが行ける範囲(マップの端−画面半分)
      int lim_left = d.width/2;
      int lim_right = map_width * CHIP_SIZE - d.width/2;
      int lim_top = d.height/2;
      int lim_bottom = map_height * CHIP_SIZE - d.height/2;

      //主人公がマップの端寄りの場合、カメラ固定
      if(ball_px + CHIP_SIZE/2 < lim_left){
         view_x = lim_left;
      }
      if(ball_px + CHIP_SIZE/2 > lim_right){
         view_x = lim_right;
      }
      if(ball_py + CHIP_SIZE/2 < lim_top){
         view_y = lim_top;
      }
      if(ball_py + CHIP_SIZE/2 > lim_bottom){
         view_y = lim_bottom;
      }

      //マップデータの範囲(主人公から6チップ分)
      int start_x;
      int start_y;
      int end_x;
      int end_y;

      start_x = view_x/CHIP_SIZE - 6;
      start_y = view_y/CHIP_SIZE - 6;
      end_x = view_x/CHIP_SIZE + 6;
      end_y = view_y/CHIP_SIZE + 6;

      //限界
      if(start_x < 0){start_x = 0;}
      if(start_y < 0){start_y = 0;}
      if(end_x > map_width){end_x = map_width;}
      if(end_y > map_height){end_y = map_height;}

      //描画位置
      int dsp_x;
      int dsp_y;

      for(i=start_y; i<end_y;i++){
         for(j=start_x; j<end_x;j++){

            //セルデータ取得
            cell_data = map_data[j][i];

            //セルデータからマップグラフィックスを取得
            gra = celltbl[ cell_data ].gra;

            //マップチップからグラフィックスの位置を取得
            x = (gra%16)*CHIP_SIZE;
            y = (gra/16)*CHIP_SIZE;

            //マップ描画位置
            dsp_x = j*CHIP_SIZE - (view_x - d.width/2);
            dsp_y = i*CHIP_SIZE - (view_y - d.height/2);

            bufferg.drawImage(map_image,dsp_x,dsp_y,
             dsp_x+CHIP_SIZE,dsp_y+CHIP_SIZE,x,y,x+CHIP_SIZE,y+CHIP_SIZE,this);
         }
      }

      //--------------ボール描画------------------
      
      //描画開始位置
      view_x = d.width/2 - CHIP_SIZE/2;
      view_y = d.height/2 - CHIP_SIZE/2;

      //マップの端に近づいてる時(カメラが行けない範囲)
      if(ball_px + CHIP_SIZE/2 < lim_left){
         view_x = (int)ball_px;
      }
      if(ball_px + CHIP_SIZE/2 > lim_right){
         view_x += (int)ball_px + CHIP_SIZE/2 - lim_right;
      }
      if(ball_py + CHIP_SIZE/2 < lim_top){
         view_y = (int)ball_py;
      }
      if(ball_py + CHIP_SIZE/2 > lim_bottom){
         view_y += (int)ball_py + CHIP_SIZE/2 - lim_bottom;
      }

      //ボール描画
      bufferg.drawImage(ball_image,view_x,view_y,this);

      //フォント
      bufferg.setColor(Color.black);
      bufferg.drawString("マップサイズ("+map_width+","+map_height+")。",10,20);
      bufferg.drawString("現在の座標はmap(" +ball_x+ "," +ball_y+ ")。" + 
       "ball(" +(int)ball_px+ "," +(int)ball_py+ ")。",10,40);
      bufferg.drawString("start(" +start_x+ "," +start_y+ ")。"+
       "end(" +end_x+ "," +end_y+ ")。",10,60);

      //ウインドウを更新する
      g.drawImage(buffer,0,0,this);
   }


   //処理
   public void action(){
      //キー状態で移動処理
      KeyForMove();

      //移動中の場合
      if(ball_moving){
         MyMoving();
      }
   }

   //キー状態で移動処理
   public void KeyForMove(){
      //上
      if(key_states[0] == 1){
         //現在移動中か?
         if(!ball_moving){
            //移動中でないなら移動
            ball_moving = true;
            ball_moving_dir = 1;
            sum_move_length = 0.0f;
         }
      }

      //下
      if(key_states[1] == 1){
         //現在移動中か?
         if(!ball_moving){
            //移動中でないなら移動
            ball_moving = true;
            ball_moving_dir = 2;
            sum_move_length = 0.0f;
         }
      }

      //左
      if(key_states[2] == 1){
         //現在移動中か?
         if(!ball_moving){
            //移動中でないなら移動
            ball_moving = true;
            ball_moving_dir = 3;
            sum_move_length = 0.0f;
         }
      }

      //右
      if(key_states[3] == 1){
         //現在移動中か?
         if(!ball_moving){
            //移動中でないなら移動
            ball_moving = true;
            ball_moving_dir = 4;
            sum_move_length = 0.0f;
         }
      }

      //Zキーを押した時
      if(key_states[4] == 2){
         //キーの状態を押し続けに変更。
         key_states[4] = 1;
      }
   }

   //移動
   public void MyMoving(){
      switch(ball_moving_dir){
      //上
      case 1:
         ball_py -= FrameTime * ball_speed;
         if(ball_py < 0.0f){ball_py = 0.0f;}

         //一定距離動いたか
         sum_move_length += FrameTime * ball_speed;
         if(sum_move_length >= (float)CHIP_SIZE){
            //ピッタリの位置にする
            ball_y--;
            if(ball_y < 0){ball_y = 0;}
            ball_py = ball_y*CHIP_SIZE;
            ball_moving = false;
         }
         break;

      //下
      case 2:
         ball_py += FrameTime * ball_speed;
         if(ball_py > map_height*CHIP_SIZE-CHIP_SIZE){ball_py = map_height*CHIP_SIZE-CHIP_SIZE;}

         //一定距離動いたか
         sum_move_length += FrameTime * ball_speed;
         if(sum_move_length >= (float)CHIP_SIZE){
            //ピッタリの位置にする
            ball_y++;
            if(ball_y > map_height-1){ball_y = map_height-1;}
            ball_py = ball_y*CHIP_SIZE;
            ball_moving = false;
         }
         break;

      //左
      case 3:
         ball_px -= FrameTime * ball_speed;
         if(ball_p < 0.0f){ball_px = 0.0f;}

         //一定距離動いたか
         sum_move_length += FrameTime * ball_speed;
         if(sum_move_length >= (float)CHIP_SIZE){
            //ピッタリの位置にする
            ball_x--;
            if(ball_x < 0){ball_x = 0;}
            ball_px = ball_x*CHIP_SIZE;
            ball_moving = false;
         }
         break;

      //右
      case 4:
         ball_px += FrameTime * ball_speed;
         if(ball_px > map_width*CHIP_SIZE-CHIP_SIZE){ball_px = map_width*CHIP_SIZE-CHIP_SIZE;}

         //一定距離動いたか
         sum_move_length += FrameTime * ball_speed;
         if(sum_move_length >= (float)CHIP_SIZE){
            //ピッタリの位置にする
            ball_x++;
            if(ball_x > map_width-1){ball_x = map_width-1;}
            ball_px = ball_x*CHIP_SIZE;
            ball_moving = false;
         }
         break;
      }
   }

   //キーが押された瞬間(使わない)
   public void keyTyped(KeyEvent e){}

   //キーを押している時
   public void keyPressed(KeyEvent e){
      int key;

      key=e.getKeyCode();

      switch(key){
      //上キー
      case KeyEvent.VK_UP:
         key_states[0] = 1;
         break;
      //下キー
      case KeyEvent.VK_DOWN:
         key_states[1] = 1;
         break;
      //左キー
      case KeyEvent.VK_LEFT:
         key_states[2] = 1;
         break;
      //右キー
      case KeyEvent.VK_RIGHT:
         key_states[3] = 1;
         break;
      //Zキー
      case KeyEvent.VK_Z:
         //最初に押した時に2を入れる
         if(key_states[4] == 0){
            key_states[4] = 2;
         }
         break;
      }
   }

   //キーが放された瞬間
   public void keyReleased(KeyEvent e){
      int key;

      key=e.getKeyCode();

      switch(key){
      //上キー
      case KeyEvent.VK_UP:
         key_states[0] = 0;
         break;
      //下キー
      case KeyEvent.VK_DOWN:
         key_states[1] = 0;
         break;
      //左キー
      case KeyEvent.VK_LEFT:
         key_states[2] = 0;
         break;
      //右キー
      case KeyEvent.VK_RIGHT:
         key_states[3] = 0;
         break;
      //Zキー
      case KeyEvent.VK_Z:
         key_states[4] = 0;
         break;
      }

   }
}