root/as3/MapChipX/trunk/src/org/libspark/mapchipx/MapChipX.as

リビジョン 2851, 5.8 kB (コミッタ: KAZUMiX, コミット時期: 3 年 前)

import

Line 
1 /*
2  * MapChipX
3  * Copyright (c) 2009 KAZUMiX
4  *
5  * Licensed under the MIT License:
6  * http://www.opensource.org/licenses/mit-license.php
7  *
8 */
9
10 package org.libspark.mapchipx {
11         import flash.display.Bitmap;
12         import flash.display.BitmapData;
13         import flash.display.Sprite;
14         import flash.geom.Point;
15         import flash.geom.Rectangle;
16        
17         public class MapChipX extends Sprite {
18                
19                 private var map:Array;
20                 private var chipInfos:ChipInfoArray;
21                 private var frameWidth:uint;
22                 private var frameHeight:uint;
23                 private var isTransparent:Boolean;
24                 private var isStaticChips:Boolean;
25                
26                 private var mapRaws:uint;
27                 private var mapCols:uint;
28                
29                 private var mapWidth:uint;
30                 private var mapHeight:uint;
31                
32                 private var chipWidth:uint;
33                 private var chipHeight:uint;
34                
35                 private var rollMap:Sprite;
36                 private var tiledBitmapContainer:Bitmap;
37                 private var tiledBitmapData:BitmapData;
38                
39                 private var tiledBitmapRadius:uint;
40
41                 private var prevTiledMapLTPoint:Point = new Point();
42
43                 public function MapChipX(map:Array, chipInfos:ChipInfoArray, frameWidth:uint, frameHeight:uint, isTransparent:Boolean=false, isStaticChips=false):void {
44                         this.map = map;
45                         this.chipInfos = chipInfos;
46                         this.frameWidth = frameWidth;
47                         this.frameHeight = frameHeight;
48                         this.isTransparent = isTransparent;
49                         this.isStaticChips = isStaticChips;
50                        
51                         this.mapRaws = this.map.length;
52                         this.mapCols = this.map[0].length;
53                        
54                         this.chipWidth = chipInfos.chipWidth;
55                         this.chipHeight = chipInfos.chipHeight;
56                        
57                         this.mapWidth = mapCols * chipWidth;
58                         this.mapHeight = mapRaws * chipHeight;
59                        
60                         this.setupRollMap();
61                         setupTiledBitmap();
62                 }
63                
64                 private function setupRollMap():void {
65                         rollMap = new Sprite();
66                         rollMap.x = this.frameWidth / 2;
67                         rollMap.y = this.frameHeight / 2;
68                         addChild(rollMap);
69                 }
70                
71                 private function setupTiledBitmap():void {
72                         tiledBitmapRadius = Math.sqrt(frameWidth * frameWidth / 4 + frameHeight * frameHeight / 4);
73                         var tiledBitmapWidth:uint = Math.ceil(tiledBitmapRadius * 2 / chipWidth + 1) * chipWidth;
74                         var tiledBitmapHeight:uint = Math.ceil(tiledBitmapRadius * 2 / chipHeight + 1) * chipHeight;
75                         tiledBitmapData = new BitmapData(tiledBitmapWidth, tiledBitmapHeight, isTransparent);
76                         tiledBitmapContainer = new Bitmap(tiledBitmapData);
77                         rollMap.addChild(tiledBitmapContainer);
78                 }
79                
80                 public function update(_centerX:int, _centerY:int, _centerRotation:Number = 0) {
81                         var centerX:int = _centerX % mapWidth;
82                         if (centerX < 0) {
83                                 centerX += mapWidth;
84                         }
85                         var centerY:int = _centerY % mapHeight;
86                         if (centerY < 0) {
87                                 centerY += mapHeight;
88                         }
89                         var centerRotation:Number = _centerRotation % 360;
90                        
91                         var visibleLTPoint:Point = getVisibleLeftTopPoint(centerX, centerY);
92                         var visibleRBPoint:Point = getVisibleRightBottomPoint(centerX, centerY);
93                        
94                         var tiledMapLTPoint:Point = getTiledMapLeftTopPoint(visibleLTPoint);
95                         var tiledMapRBPoint:Point = getTileMapRightBottomPoint(visibleRBPoint);
96                        
97                         setTiledBitmap(tiledMapLTPoint, tiledMapRBPoint);
98                         setTiledBitmapPosition(centerX, centerY, tiledMapLTPoint);
99                        
100                         rollMap.rotation = centerRotation;
101                 }
102                
103                 private function getVisibleLeftTopPoint(centerX:int, centerY:int):Point {
104                         var x:Number = centerX - tiledBitmapRadius;
105                         var y:Number = centerY - tiledBitmapRadius;
106                         return new Point(x, y);
107                 }
108                
109                 private function getVisibleRightBottomPoint(centerX:int, centerY:int):Point {
110                         var x:Number = centerX + tiledBitmapRadius;
111                         var y:Number = centerY + tiledBitmapRadius;
112                         return new Point(x, y);
113                 }
114                
115                 private function getTiledMapLeftTopPoint(visibleLTPoint:Point):Point {
116                         var x:Number = Math.floor(visibleLTPoint.x / chipWidth);
117                         var y:Number = Math.floor(visibleLTPoint.y / chipHeight);
118                         return new Point(x, y);
119                 }
120                
121                 private function getTileMapRightBottomPoint(visibleRBPoint:Point):Point {
122                         var x:Number = Math.ceil(visibleRBPoint.x / chipWidth);
123                         var y:Number = Math.ceil(visibleRBPoint.y / chipHeight);
124                         return new Point(x, y);
125                 }
126                
127                 private function setTiledBitmap(tiledMapLTPoint:Point, tiledMapRBPoint:Point):void {
128                         if (isStaticChips && isSameBlock(tiledMapLTPoint)) {
129                                 return;
130                         }
131                         prevTiledMapLTPoint = tiledMapLTPoint;
132                        
133                         var rawOffset:int = tiledMapLTPoint.y;
134                         var colOffset:int = tiledMapLTPoint.x;
135                        
136                         for (var raw:int = tiledMapLTPoint.y, raws:int = tiledMapRBPoint.y; raw <= raws; raw++) {
137                                 var tileY:uint = (raw - rawOffset) * chipHeight;
138                                 for ( var col:int = tiledMapLTPoint.x, cols:int = tiledMapRBPoint.x; col <= cols; col++) {
139                                         var chipInfo:ChipInfo = getChipInfoFromMap(raw, col);
140                                         var tileX:uint = (col - colOffset) * chipWidth;
141                                         chipInfo.pasteTo(tiledBitmapData, new Point(tileX, tileY));
142                                 }
143                         }
144                 }
145                
146                 private function isSameBlock(tiledMapLTPoint:Point):Boolean {
147                         if (prevTiledMapLTPoint.x == tiledMapLTPoint.x && prevTiledMapLTPoint.y == tiledMapLTPoint.y) {
148                                 return true;
149                         }
150                         return false;
151                 }
152                
153                 private function getChipInfoFromMap(raw:int, col:int):ChipInfo {
154                         var x:int = col % mapCols;
155                         if (x < 0) {
156                                 x += mapCols;
157                         }
158                         var y:int = raw % mapRaws;
159                         if (y < 0) {
160                                 y += mapRaws;
161                         }
162                         //trace(y, x);
163                         return chipInfos[map[y][x]];
164                 }
165                
166                 private function setTiledBitmapPosition(centerX:int, centerY:int, tiledMapLTPoint:Point):void {
167                         var centerTileX:int = centerX / chipWidth;
168                         var tileOffsetX:int = (tiledMapLTPoint.x - centerTileX) * chipWidth;
169                         var modX:int = centerX % chipWidth;
170                         var x:int = tileOffsetX - modX;
171                        
172                         var centerTileY:int = centerY / chipHeight;
173                         var tileOffsetY:int = (tiledMapLTPoint.y - centerTileY) * chipHeight;
174                         var modY:int = centerY % chipHeight;
175                         var y:int = tileOffsetY - modY;
176                        
177                         tiledBitmapContainer.x = x;
178                         tiledBitmapContainer.y = y;
179                 }
180                
181         }
182        
183 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。