引言
丰富的关卡与场景是充实游戏的魔法圣器,时而穿过云霄,时而坠入大海,就算是陆地同样可以云雾缭绕、山峦叠嶂;作为玩家,游戏玩累了休息时聆听的可以不仅仅是音乐,作为游戏设计者,你有责任将此时疲惫的他们带进梦幻空间:登上紫禁之颠、长城尽头,潜入亚特兰蒂斯深处与美人鱼结伴嬉戏,尝试一次惬意舒心的休憩之旅又未尝不可?虚幻的游戏同样可以给玩家带来真切的感受,华丽莫测的场景变换开启了这扇通往意念领域的大门。
8.1游戏中场景切换实现(交叉参考:地图间的传送与切换 梦幻西游(Demo) 之 “天人合一”① )
传统游戏两个场景之间切换往往通过呈现一幅游戏相关的宣传背景作为过度,并更新地图、角色、模型等目标场景所必须的一切资源读取加载完毕后才算完成。通过Silverlight开发基于Web的MMORPG网页游戏则可简化这一过程,动态按需下载技术使得我们在进入新场景前仅需下载该场景的配置文件及缩略地图等少部分资源即可。
按照该思路,我们首先创建一个名为Transition的过场类:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
///
<summary>
/// 过场控件
/// </summary>
public sealed class Transition : Canvas {
int _Code = - 1 ;
/// <summary>
/// 获取或设置代号
/// </summary>
public int Code {
get { return _Code; }
set {
if (_Code != value) {
_Code = value;
this .Background = new ImageBrush() {
ImageSource = Global.GetProjectImage( string .Format( " Transition/{0}.jpg " , value))
};
}
}
}
/// <summary>
/// 获取或设置X、Y坐标
/// </summary>
public Point Coordinate {
get { return new Point(Canvas.GetLeft( this ) + Center.X, Canvas.GetTop( this ) + Center.Y); }
set { Canvas.SetLeft( this , value.X - Center.X); Canvas.SetTop( this , value.Y - Center.Y); }
}
/// <summary>
/// 获取或设置Z层次深度
/// </summary>
public int Z {
get { return Canvas.GetZIndex( this ); }
set { Canvas.SetZIndex( this , value); }
}
/// <summary>
/// 获取或设置中心
/// </summary>
public Point Center { get ; set ; }
/// <summary>
/// 适应游戏窗口尺寸
/// </summary>
public void AdaptToWindowSize() {
this .Width = Application.Current.Host.Content.ActualWidth;
this .Height = Application.Current.Host.Content.ActualHeight;
}
public Transition() {
this .CacheMode = new BitmapCache();
}
}
/// 过场控件
/// </summary>
public sealed class Transition : Canvas {
int _Code = - 1 ;
/// <summary>
/// 获取或设置代号
/// </summary>
public int Code {
get { return _Code; }
set {
if (_Code != value) {
_Code = value;
this .Background = new ImageBrush() {
ImageSource = Global.GetProjectImage( string .Format( " Transition/{0}.jpg " , value))
};
}
}
}
/// <summary>
/// 获取或设置X、Y坐标
/// </summary>
public Point Coordinate {
get { return new Point(Canvas.GetLeft( this ) + Center.X, Canvas.GetTop( this ) + Center.Y); }
set { Canvas.SetLeft( this , value.X - Center.X); Canvas.SetTop( this , value.Y - Center.Y); }
}
/// <summary>
/// 获取或设置Z层次深度
/// </summary>
public int Z {
get { return Canvas.GetZIndex( this ); }
set { Canvas.SetZIndex( this , value); }
}
/// <summary>
/// 获取或设置中心
/// </summary>
public Point Center { get ; set ; }
/// <summary>
/// 适应游戏窗口尺寸
/// </summary>
public void AdaptToWindowSize() {
this .Width = Application.Current.Host.Content.ActualWidth;
this .Height = Application.Current.Host.Content.ActualHeight;
}
public Transition() {
this .CacheMode = new BitmapCache();
}
}
并在场景类中定义两个事件ChangeStart和ChangeEnd分别放在场景代号改变时及Mini地图下载完毕后:
int
_Code
=
-
1
;
/// <summary>
/// 获取或设置代号
/// </summary>
public int Code {
get { return _Code; }
set {
if (_Code != value) {
_Code = value;
if (ChangeStart != null ) { ChangeStart( this , null ); }
teleports.Clear(); // 清空传送点集合
ClearMasks(); // 清空遮挡物
ClearSprites(); // 清空精灵
ClearAnimations(); // 清空动画
Downloader configDownloader = new Downloader() { TargetCode = value };
configDownloader.Completed += new EventHandler < DownloaderEventArgs > (configDownloader_Completed);
configDownloader.Download(Global.WebPath( string .Format( " Scene/{0}/Info.xml " , value)));
}
}
}
/// <summary>
/// Mini地图背景下载完毕
/// </summary>
void miniMapDownloader_Completed( object sender, DownloaderEventArgs e) {
Downloader miniMapDownloader = sender as Downloader;
miniMapDownloader.Completed -= miniMapDownloader_Completed;
int code = miniMapDownloader.TargetCode;
// 用缩略图填充地图背景(如果异步与同步一致)
if (miniMapDownloader.Index == index) { map.Source = Global.GetWebImage( string .Format( " Scene/{0}/MiniMap.jpg " , code)); }
// 下载实际地图
Downloader realMapDownloader = new Downloader() { TargetCode = code, Index = miniMapDownloader.Index };
realMapDownloader.Completed += new EventHandler < DownloaderEventArgs > (realMapDownloader_Completed);
realMapDownloader.Download(Global.WebPath( string .Format( " Scene/{0}/RealMap.jpg " , code)));
if (ChangeEnd != null ) { ChangeEnd( this , null ); }
}
/// <summary>
/// 实际地图背景下载完毕
/// </summary>
/// <summary>
/// 获取或设置代号
/// </summary>
public int Code {
get { return _Code; }
set {
if (_Code != value) {
_Code = value;
if (ChangeStart != null ) { ChangeStart( this , null ); }
teleports.Clear(); // 清空传送点集合
ClearMasks(); // 清空遮挡物
ClearSprites(); // 清空精灵
ClearAnimations(); // 清空动画
Downloader configDownloader = new Downloader() { TargetCode = value };
configDownloader.Completed += new EventHandler < DownloaderEventArgs > (configDownloader_Completed);
configDownloader.Download(Global.WebPath( string .Format( " Scene/{0}/Info.xml " , value)));
}
}
}
/// <summary>
/// Mini地图背景下载完毕
/// </summary>
void miniMapDownloader_Completed( object sender, DownloaderEventArgs e) {
Downloader miniMapDownloader = sender as Downloader;
miniMapDownloader.Completed -= miniMapDownloader_Completed;
int code = miniMapDownloader.TargetCode;
// 用缩略图填充地图背景(如果异步与同步一致)
if (miniMapDownloader.Index == index) { map.Source = Global.GetWebImage( string .Format( " Scene/{0}/MiniMap.jpg " , code)); }
// 下载实际地图
Downloader realMapDownloader = new Downloader() { TargetCode = code, Index = miniMapDownloader.Index };
realMapDownloader.Completed += new EventHandler < DownloaderEventArgs > (realMapDownloader_Completed);
realMapDownloader.Download(Global.WebPath( string .Format( " Scene/{0}/RealMap.jpg " , code)));
if (ChangeEnd != null ) { ChangeEnd( this , null ); }
}
/// <summary>
/// 实际地图背景下载完毕
/// </summary>