// overlapsWithCallBack public Boolean overlapsWithCallback(FlxObject Object, Func <FlxObject, FlxObject, Boolean> Callback = null, Boolean FlipCallbackParams = false, FlxPoint Position = null) { Boolean results = false; float X = x; float Y = y; if (Position != null) { X = Position.x; Y = Position.x; } //Figure out what tiles we need to check against int selectionX = (int)FlxU.floor((Object.x - X) / _tileWidth); int selectionY = (int)FlxU.floor((Object.y - Y) / _tileHeight); uint selectionWidth = (uint)(selectionX + (FlxU.ceil((int)Object.width / _tileWidth)) + 2); uint selectionHeight = (uint)(selectionY + (FlxU.ceil((int)Object.height / _tileHeight)) + 2); //Then bound these coordinates by the map edges if (selectionX < 0) { selectionX = 0; } if (selectionY < 0) { selectionY = 0; } if (selectionWidth > widthInTiles) { selectionWidth = (uint)widthInTiles; } if (selectionHeight > heightInTiles) { selectionHeight = (uint)heightInTiles; } //Then loop through this selection of tiles and call FlxObject.separate() accordingly uint rowStart = (uint)selectionY * (uint)widthInTiles; uint row = (uint)selectionY; uint column; FlxTile tile; Boolean overlapFound; float deltaX = X - last.x; float deltaY = Y - last.y; while (row < selectionHeight) { column = (uint)selectionX; while (column < selectionWidth) { overlapFound = false; tile = _tileObjects[(int)_data[(int)(rowStart + column)]] as FlxTile; if (Convert.ToBoolean(tile.allowCollisions)) { tile.x = X + (int)column * _tileWidth; tile.y = Y + (int)row * _tileHeight; tile.last.x = tile.x - deltaX; tile.last.y = tile.y - deltaY; if (Callback != null) { if (FlipCallbackParams) { overlapFound = Callback(Object, tile); } else { overlapFound = Callback(tile, Object); } } else { overlapFound = (Object.x + Object.width > tile.x) && (Object.x < tile.x + tile.width) && (Object.y + Object.height > tile.y) && (Object.y < tile.y + tile.height); } if (overlapFound) { if ((tile.callback != null)) { tile.mapIndex = (uint)rowStart + column; tile.callback(tile, Object); } results = true; } } else if ((tile.callback != null)) { tile.mapIndex = (uint)rowStart + (uint)column; tile.callback(tile, Object); } column++; } rowStart += (uint)widthInTiles; row++; } return(results); }
/** * Shoots a ray from the start point to the end point. * If/when it passes through a tile, it stores that point and returns false. * * @param Start The world coordinates of the start of the ray. * @param End The world coordinates of the end of the ray. * @param Result A <code>Point</code> object containing the first wall impact. * @param Resolution Defaults to 1, meaning check every tile or so. Higher means more checks! * @return Returns true if the ray made it from Start to End without hitting anything. Returns false and fills Result if a tile was hit. */ public bool ray(FlxPoint Start, FlxPoint End, FlxPoint Result = null, float Resolution = 1f) { float step = _tileWidth; if (_tileHeight < _tileWidth) { step = _tileHeight; } step /= Resolution; float deltaX = End.x - Start.x; float deltaY = End.y - Start.y; float distance = (float)Math.Sqrt(deltaX * deltaX + deltaY * deltaY); int steps = (int)FlxU.ceil(distance / step); float stepX = deltaX / steps; float stepY = deltaY / steps; float curX = Start.x - stepX - x; float curY = Start.y - stepY - y; int tileX; int tileY; int i = 0; while (i < steps) { curX += stepX; curY += stepY; if ((curX < 0) || (curX > width) || (curY < 0) || (curY > height)) { i++; continue; } tileX = (int)curX / _tileWidth; tileY = (int)curY / _tileHeight; if (Convert.ToBoolean((_tileObjects[_data[tileY * widthInTiles + tileX]] as FlxTile).allowCollisions)) { //Some basic helper stuff tileX *= _tileWidth; tileY *= _tileHeight; float rx = 0; float ry = 0; float q; float lx = curX - stepX; float ly = curY - stepY; //Figure out if it crosses the X boundary q = tileX; if (deltaX < 0) { q += _tileWidth; } rx = q; ry = ly + stepY * ((q - lx) / stepX); if ((ry > tileY) && (ry < tileY + _tileHeight)) { if (Result == null) { Result = new FlxPoint(); } Result.x = rx; Result.y = ry; return(false); } //Else, figure out if it crosses the Y boundary q = tileY; if (deltaY < 0) { q += _tileHeight; } rx = lx + stepX * ((q - ly) / stepY); ry = q; if ((rx > tileX) && (rx < tileX + _tileWidth)) { if (Result == null) { Result = new FlxPoint(); } Result.x = rx; Result.y = ry; return(false); } return(true); } i++; } return(true); }
// update camera scroll in here // make sure it stays within bounds public override void update() { //zooming = FlxG.zoom; //rotating = FlxG.rotation; //follow closely or check deadzones if (target != null) { if (deadzone == null) { focusOn(target.getMidpoint()); //add getMidpoint() for FlxObjects } else { //FlxG.log("deadzone is not null"); float edge; float targetX = FlxU.ceil(target.x + ((target.x > 0) ? 0.0000001f : -0.0000001f)); float targetY = FlxU.ceil(target.y + ((target.y > 0) ? 0.0000001f : -0.0000001f)); edge = targetX - deadzone.x; if (scroll.x > edge) { scroll.x = edge; } edge = targetX + target.width - deadzone.x - deadzone.width; if (scroll.x < edge) { scroll.x = edge; } edge = targetY - deadzone.y; if (scroll.y > edge) { scroll.y = edge; } edge = targetY + target.height - deadzone.y - deadzone.height; if (scroll.y < edge) { scroll.y = edge; } //SHAKE } } //make sure we didnt go outside camera's bounds if (bounds != null) { //FlxG.log("bounds is not null"); if (scroll.x < bounds.left) { scroll.x = bounds.left; } if (scroll.x > bounds.right - width) { scroll.x = bounds.right - width; } if (scroll.y < bounds.top) { scroll.y = bounds.top; } if (scroll.y > bounds.bottom - height) { scroll.y = bounds.bottom - height; } } //update effects //shake if (_fxShakeDuration > 0) { _fxShakeDuration -= FlxG.elapsed; if (_fxShakeDuration <= 0) { _fxShakeOffset.make(); if (_fxShakeComplete != null) { _fxShakeComplete(); } } else { if ((_fxShakeDirection == SHAKE_BOTH_AXES) || (_fxShakeDirection == SHAKE_HORIZONTAL_ONLY)) { _fxShakeOffset.x = (FlxG.random() * _fxShakeIntensity * width * 2 - _fxShakeIntensity * width) * _zoom; } if ((_fxShakeDirection == SHAKE_BOTH_AXES) || (_fxShakeDirection == SHAKE_VERTICAL_ONLY)) { _fxShakeOffset.y = (FlxG.random() * _fxShakeIntensity * height * 2 - _fxShakeIntensity * height) * _zoom; } } } scroll.x -= _fxShakeOffset.x; scroll.y -= _fxShakeOffset.y; if (zooming < 1) { zooming = 1; } }