Changeset 12152 for lang/java/NanikaKit

Show
Ignore:
Timestamp:
05/22/08 00:20:08 (5 years ago)
Author:
tarchan
Message:

lang/java/NanikaKit: 立ち絵の当たり判定を実装 #92

Location:
lang/java/NanikaKit/trunk/src/com/mac/tarchan/nanika
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • lang/java/NanikaKit/trunk/src/com/mac/tarchan/nanika/SakuraGhost.java

    r11027 r12152  
    1111import java.awt.EventQueue; 
    1212import java.awt.Graphics2D; 
     13import java.awt.Point; 
    1314import java.awt.Rectangle; 
    1415import java.awt.event.ActionEvent; 
    1516import java.awt.event.MouseEvent; 
    1617import java.awt.event.MouseWheelEvent; 
    17 import java.awt.geom.AffineTransform; 
    1818import java.awt.image.BufferedImage; 
    1919import java.io.File; 
    2020import java.io.IOException; 
    2121import java.util.LinkedHashMap; 
     22import java.util.Map; 
    2223import java.util.Properties; 
    2324import java.util.concurrent.Executors; 
     
    4950 
    5051        /** シェル */ 
    51         private LinkedHashMap<Integer, SakuraShell> shell = new LinkedHashMap<Integer, SakuraShell>(); 
     52        private LinkedHashMap<Integer, SakuraShell> shells = new LinkedHashMap<Integer, SakuraShell>(); 
    5253 
    5354        /** NAR ファイル */ 
     
    188189                        { 
    189190                                if (popupMenu.isVisible()) return; 
     191 
     192                                // 当たり判定 
     193                                hit(mouseevent.getPoint()); 
    190194 
    191195//                              reset(); 
     
    371375 
    372376                // sakura 
    373                 shell.put(0, sakura); 
     377                shells.put(0, sakura); 
    374378//              shell.get(0).setSurface(0); 
    375379 
    376380                // kero 
    377                 shell.put(1, kero); 
     381                shells.put(1, kero); 
    378382//              shell.get(1).setSurface(10); 
    379383        } 
     
    387391        { 
    388392                log.info("close"); 
    389                 shell.clear(); 
     393                shells.clear(); 
    390394                nar = null; 
    391395                currentShell = null; 
     
    403407        { 
    404408                log.info("vanish"); 
    405                 shell.clear(); 
     409                shells.clear(); 
    406410                nar = null; 
    407411                currentShell = null; 
     
    434438        { 
    435439                log.debug("scope=" + scope); 
    436                 currentShell = shell.get(scope); 
     440                currentShell = shells.get(scope); 
    437441 
    438442                return this; 
     
    522526        public SakuraGhost talk(String message) 
    523527        { 
    524                 log.debug(message); 
     528//              log.debug(message); 
    525529//              if (currentShell.getBalloon() != null) 
    526530//              { 
     
    601605        public void draw(Graphics2D g) 
    602606        { 
    603                 SakuraShell sakura = shell.get(0); 
    604                 SakuraShell kero = shell.get(1); 
     607                SakuraShell sakura = shells.get(0); 
     608                SakuraShell kero = shells.get(1); 
    605609 
    606610                Rectangle clip = g.getClipBounds(); 
    607                 log.debug("clip=" + clip); 
     611//              log.debug("clip=" + clip); 
    608612                int x = clip.x; 
    609613                int y = clip.y; 
     
    614618                if (sakura != null && sakura.getSurface() != null) 
    615619                { 
    616                         AffineTransform tx = new AffineTransform(); 
     620//                      AffineTransform tx = new AffineTransform(); 
    617621//                      tx.scale(0.8, 0.8); 
    618622//                      Rectangle rect = sakura.getBounds(); 
    619                         Rectangle rect = tx.createTransformedShape(sakura.getSurface()).getBounds(); 
     623//                      Rectangle rect = tx.createTransformedShape(sakura.getSurface()).getBounds(); 
     624                        Rectangle rect = sakura.getSurface().getBounds(); 
    620625//                      log.debug("rect=" + rect); 
    621626                        rect.x = right - rect.width; 
    622627                        rect.y = bottom - rect.height; 
    623628//                      tx.shear(-0.5, 0); 
    624                         tx.rotate(Math.toRadians(0), right - rect.width / 2, bottom); 
    625                         tx.translate(rect.x, rect.y); 
    626                         g.setTransform(tx); 
     629//                      tx.rotate(Math.toRadians(0), right - rect.width / 2, bottom); 
     630//                      tx.translate(rect.x, rect.y); 
     631//                      g.setTransform(tx); 
     632                        sakura.setLocation(rect.getLocation()); 
    627633                        sakura.draw(g); 
    628634                        right = rect.x; 
     
    632638                if (kero != null && kero.getSurface() != null) 
    633639                { 
    634                         AffineTransform tx = new AffineTransform(); 
     640//                      AffineTransform tx = new AffineTransform(); 
    635641                        Rectangle rect = kero.getSurface().getBounds(); 
    636642//                      rect.x = x + (right - x) / 2 - rect.width / 2; 
    637643                        rect.x = right - rect.width; 
    638644                        rect.y = bottom - rect.height; 
    639                         tx.translate(rect.x, rect.y); 
     645//                      tx.translate(rect.x, rect.y); 
    640646//                      tx.shear(0.5, 0); 
    641                         g.setTransform(tx); 
     647//                      g.setTransform(tx); 
     648                        kero.setLocation(rect.getLocation()); 
    642649                        kero.draw(g); 
    643650                } 
     
    662669 
    663670        /** 
     671         * 当たり判定を確認します。 
     672         *  
     673         * @param p クリック位置 
     674         */ 
     675        public void hit(Point p) 
     676        { 
     677                System.out.println("click: " + p); 
     678                log.debug("hit: " + p + ", shell: " + shells); 
     679                for (Map.Entry<Integer, SakuraShell> entry : shells.entrySet()) 
     680                { 
     681                        int id = entry.getKey(); 
     682                        SakuraShell s = entry.getValue(); 
     683                        String name = s.hit(p); 
     684                        if (name != null) System.out.println("hit! " + name); 
     685                        log.debug("id: " + id + ", shell: " + s); 
     686                } 
     687        } 
     688 
     689        /** 
    664690         * サムネールを返します。 
    665691         *  
  • lang/java/NanikaKit/trunk/src/com/mac/tarchan/nanika/SakuraShell.java

    r9435 r12152  
    99 
    1010import java.awt.Graphics2D; 
     11import java.awt.Point; 
     12import java.awt.geom.AffineTransform; 
    1113import java.io.File; 
    1214import java.io.IOException; 
     
    7274        private SakuraBalloon balloon; 
    7375 
     76        /** 表示座標 */ 
     77        private Point loc = new Point(); 
     78 
    7479        /** 
    7580         * シェルを構築します。 
     
    264269 
    265270        /** 
     271         * 表示座標を設定します。 
     272         *  
     273         * @param p 表示座標 
     274         */ 
     275        public void setLocation(Point p) 
     276        { 
     277                loc.setLocation(p); 
     278        } 
     279 
     280        /** 
    266281         * サーフェスを描画します。 
    267282         *  
     
    270285        public void draw(Graphics2D g) 
    271286        { 
     287                AffineTransform tx = new AffineTransform(); 
     288                tx.translate(loc.x, loc.y); 
     289                g.setTransform(tx); 
     290 
    272291                if (surface != null) surface.draw(g); 
    273292                if (balloon != null) balloon.draw(g); 
     293        } 
     294 
     295        /** 
     296         * 当たり判定します。 
     297         *  
     298         * @param p 当たり判定座標 
     299         * @return 当たりの場合は当たった部分の名前。そうでない場合は null 
     300         */ 
     301        public String hit(Point p) 
     302        { 
     303                return surface != null ? surface.hit(p.x - loc.x, p.y - loc.y) : null; 
    274304        } 
    275305 
  • lang/java/NanikaKit/trunk/src/com/mac/tarchan/nanika/SakuraSurface.java

    r11027 r12152  
    6464 
    6565        /** 
    66          * サーフェスをロードします。 
     66         * サーフェスを構築します。 
    6767         *  
    6868         * @param id サーフェス ID 
    69          * @param nar NAR アーカイブ 
    70          * @return サーフェス 
    71          * @throws IOException 入力エラーが発生した場合 
    72          */ 
    73         public static SakuraSurface getSurface(int id, NanikaArchive nar) throws IOException 
    74         { 
    75                 File file = new File(nar.getShellDirectory(), String.format("surface%s.png", id)); 
    76                 log.debug(id + "=" + file); 
    77                 NanikaEntry entry = nar.getEntry(file); 
    78 //              log.debug(id + "=" + entry.getName()); 
    79  
    80                 String descript = loadDescript(id, nar); 
    81                 BufferedImage image = ImageIO.read(entry.getInputStream()); 
    82 //              log.debug("image=" + image); 
    83 //              log.debug("image=" + image.getWidth() + "x" + image.getHeight() + ","  + image.getType() + "," + image.getColorModel()); 
    84 //              int rgb = image.getRGB(0, 0); 
    85 //              log.debug("rgb=0x" + Integer.toHexString(rgb)); 
    86                 SakuraSurface surface = new SakuraSurface("" + id, image, descript); 
    87                 return surface; 
    88         } 
     69         * @param image サーフェスイメージ 
     70         */ 
     71        public SakuraSurface(String id, BufferedImage image) 
     72        { 
     73                this(id, image, null); 
     74        } 
     75 
     76        /** 
     77         * サーフェスを構築します。 
     78         *  
     79         * @param id サーフェス ID 
     80         * @param image サーフェスイメージ 
     81         * @param descript サーフェス定義 
     82         */ 
     83        public SakuraSurface(String id, BufferedImage image, String descript) 
     84        { 
     85                this.id = id; 
     86                this.image = image; 
     87                this.rect = new Rectangle(0, 0, image.getWidth(), image.getHeight()); 
     88                if (descript != null) parseDescript(descript); 
     89        } 
     90 
     91        /** 
     92                 * サーフェスをロードします。 
     93                 *  
     94                 * @param id サーフェス ID 
     95                 * @param nar NAR アーカイブ 
     96                 * @return サーフェス 
     97                 * @throws IOException 入力エラーが発生した場合 
     98                 */ 
     99                public static SakuraSurface getSurface(int id, NanikaArchive nar) throws IOException 
     100                { 
     101                        File file = new File(nar.getShellDirectory(), String.format("surface%s.png", id)); 
     102                        log.debug(id + "=" + file); 
     103                        NanikaEntry entry = nar.getEntry(file); 
     104        //              log.debug(id + "=" + entry.getName()); 
     105         
     106                        String descript = loadDescript(id, nar); 
     107                        BufferedImage image = ImageIO.read(entry.getInputStream()); 
     108        //              log.debug("image=" + image); 
     109        //              log.debug("image=" + image.getWidth() + "x" + image.getHeight() + ","  + image.getType() + "," + image.getColorModel()); 
     110        //              int rgb = image.getRGB(0, 0); 
     111        //              log.debug("rgb=0x" + Integer.toHexString(rgb)); 
     112                        SakuraSurface surface = new SakuraSurface("" + id, image, descript); 
     113                        return surface; 
     114                } 
    89115 
    90116        /** 
     
    124150                        return null; 
    125151                } 
    126         } 
    127  
    128         /** 
    129          * サーフェスを構築します。 
    130          *  
    131          * @param id サーフェス ID 
    132          * @param image サーフェスイメージ 
    133          */ 
    134         public SakuraSurface(String id, BufferedImage image) 
    135         { 
    136                 this(id, image, null); 
    137         } 
    138  
    139         /** 
    140          * サーフェスを構築します。 
    141          *  
    142          * @param id サーフェス ID 
    143          * @param image サーフェスイメージ 
    144          * @param descript サーフェス定義 
    145          */ 
    146         public SakuraSurface(String id, BufferedImage image, String descript) 
    147         { 
    148                 this.id = id; 
    149                 this.image = image; 
    150                 this.rect = new Rectangle(0, 0, image.getWidth(), image.getHeight()); 
    151                 if (descript != null) parseDescript(descript); 
    152152        } 
    153153 
     
    176176                                Rectangle rect = new Rectangle(new Point(x1, y1)); 
    177177                                rect.add(new Point(x2, y2)); 
    178                                 System.out.println("当たり判定: " + head + ": " + name + ": " + rect); 
     178                                log.debug("当たり判定: " + head + ": " + name + ": " + rect); 
    179179                                collisions.put(name, rect); 
    180180                        } 
     
    191191                                int y = Integer.parseInt(token[3]); 
    192192                                Point p = new Point(x, y); 
    193                                 System.out.println("ベースサーフェス: " + head + ": " + Arrays.toString(new Object[]{type, filename, p})); 
     193                                log.debug("ベースサーフェス: " + head + ": " + Arrays.toString(new Object[]{type, filename, p})); 
    194194                        } 
    195195                        else if (s.hasNext("(.+?interval),(.+)")) 
     
    199199                                String head = m.group(1); 
    200200                                String body = m.group(2); 
    201                                 System.out.println("アニメーション開始: " + head + ": " + Arrays.toString(body.split(","))); 
     201                                log.debug("アニメーション開始: " + head + ": " + Arrays.toString(body.split(","))); 
    202202                        } 
    203203                        else if (s.hasNext("(.+?pattern.+?),(.+)")) 
     
    207207                                String head = m.group(1); 
    208208                                String body = m.group(2); 
    209                                 System.out.println("アニメーションパターン: " + head + ": " + Arrays.toString(body.split(","))); 
     209                                log.debug("アニメーションパターン: " + head + ": " + Arrays.toString(body.split(","))); 
    210210                        } 
    211211                        else if (s.hasNext("(.+?option),(.+)")) 
     
    215215                                String head = m.group(1); 
    216216                                String body = m.group(2); 
    217                                 System.out.println("オプション: " + head + ": " + Arrays.toString(body.split(","))); 
     217                                log.debug("オプション: " + head + ": " + Arrays.toString(body.split(","))); 
    218218                        } 
    219219                        else if (s.hasNextLine()) 
    220220                        { 
    221221                                String line = s.nextLine(); 
    222                                 if (line.trim().length() > 0) System.out.println("未定義: " + line); 
     222                                if (line.trim().length() > 0) log.debug("未定義: " + line); 
    223223                        } 
    224224                        else 
     
    248248        { 
    249249                rect.setLocation(x, y); 
    250         } 
    251  
    252         /** 
    253          * サーフェスの文字列表現を返します。 
    254          *  
    255          * @return サーフェスの文字列表現 
    256          */ 
    257         public String toString() 
    258         { 
    259                 return String.format("%s[%d, %d, %dx%d, 0x%x]", id, rect.x, rect.y, rect.width, rect.height, image.getRGB(0, 0)); 
    260250        } 
    261251 
     
    313303        public String hit(int x, int y) 
    314304        { 
     305//              System.out.println("location: " + x + ", " + y); 
     306//              System.out.println("shape: " + rect); 
    315307                for (Map.Entry<String, Rectangle> entry : collisions.entrySet()) 
    316308                { 
    317309                        String name = entry.getKey(); 
    318310                        Rectangle rect = entry.getValue(); 
     311//                      System.out.println("hit? " + name + ", " + rect); 
    319312                        if (rect.contains(x, y)) return name; 
    320313                } 
    321314 
    322315                return null; 
     316        } 
     317 
     318        /** 
     319         * サーフェスの文字列表現を返します。 
     320         *  
     321         * @return サーフェスの文字列表現 
     322         */ 
     323        public String toString() 
     324        { 
     325                return String.format("%s[%d, %d, %dx%d, 0x%x]", id, rect.x, rect.y, rect.width, rect.height, image.getRGB(0, 0)); 
    323326        } 
    324327