分类: C/C++
2015-12-21 11:20:40
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
|
if (Input.GetKey(KeyCode.D))
{
…设置方向向右..
if (Input.GetKey(KeyCode.LeftShift))
{
..移动..播放跑步动画..
}
else
{
..移动..播放走路动画..
}
}
else if (Input.GetKey(KeyCode.A))
{
…设置方向向左..
if (Input.GetKey(KeyCode.LeftShift))
{
..移动..播放跑步动画..
}
else
{
..移动..播放走路动画..
}
}
|
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
|
bool isJump = false;
if (Input.GetKeyDown(KeyCode.W))
{
isJump = true;
}
if (Input.GetKey(KeyCode.D))
{
…设置方向向右..
if (Input.GetKey(KeyCode.LeftShift))
{
if(!isJump)
..移动..播放跑步动画..
else
..移动..播放跳跃动画..
}
else
{
if(!isJump)
..移动..播放走路动画..
else
..移动..播放跳跃动画..
}
}
else if (Input.GetKey(KeyCode.A))
{
…设置方向向左..
if(!isJump)
..移动..播放跑步动画..
else
..移动..播放跳跃动画..
}
else
{
if(!isJump)
..移动..播放走路动画..
else
..移动..播放跳跃动画..
}
}
|
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
|
bool isCrouch = false;
if (Input.GetKeyDown(KeyCode.S))
{
isCrouch = true;
}
bool isJump = false;
if (Input.GetKeyDown(KeyCode.W)&&! isCrouch)
{
isJump = true;
}
if (Input.GetKey(KeyCode.D))
{
…设置方向向右..
if (Input.GetKey(KeyCode.LeftShift))
{
if(!isJump&&! isCrouch)
..移动..播放跑步动画..
else if(!isCrouch)
..移动..播放跳跃动画..
else
..移动..播放蹲走动画..
}
else
{
if(!isJump&&! isCrouch)
..移动..播放跑步动画..
else if(!isCrouch)
..移动..播放跳跃动画..
else
..移动..播放蹲走动画.. }
}
else if (Input.GetKey(KeyCode.A))
{
…设置方向向左..
if(!isJump&&! isCrouch)
..移动..播放跑步动画..
else if(!isCrouch)
..移动..播放跳跃动画..
else
..移动..播放蹲走动画..
}
else
{
if(!isJump&&! isCrouch)
..移动..播放跑步动画..
else if(!isCrouch)
..移动..播放跳跃动画..
else
..移动..播放蹲走动画..
}
}
|
阿兰图灵提出的图灵机就是一种状态机,就是指一个抽象的机器,它有一条无限长的纸带TAPE,纸带分成了一个一个的小方格,每个方格有不同的颜色。有一个读写头HEAD在纸带上移来移去。机器头有 一组内部状态,还有一些固定的程序。在每个时刻,机器头都要从当前纸带上读入一个方格信息,然后结合自己的内部状态查找程序表,根据程序输出信息到纸带方格上,并转换自己的内部状态,然后进行移动。
1
2
3
4
5
6
7
8
9
|
public enum CharacterState
{
Idling = 0,
Walking = 1,
Jumping = 2,
acting= 3,
defending= 4,
}
public CharacterState heroState = CharacterState. Idling;
|
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
|
void handleInput()
{
switch(heroState)
{
case CharacterState. Idling:
…播放空闲动画..
if…Input.GetKey –A,D.
this. heroState = CharacterState. Walking;
else if…Input.GetKey –w.
this. heroState = CharacterState. Jumping;
else if…Input.GetKey –J.
this. heroState = CharacterState. acting;
else if…Input.GetKey –I.
this. heroState = CharacterState. defending;
break;
case CharacterState. Walking:
if…Input.GetKey –A,D.
…CharacterController移动操作..
else…Input.GetKeyUp – A,D…
this. heroState = CharacterState. Idling;
break;
case CharacterState. Jumping:
if(Input.GetKeyUp(KeyCode.W))
…CharacterController移动操作..
if(CharacterController.isGrounded)
{
this. heroState = CharacterState. Idling;
}
break;
case CharacterState. acting:
…播放攻击动画.
chargeTime += Time.timeScale / Time.deltaTime;
if(chargeTime>maxTime)
{
this. heroState = CharacterState. Idling;
chargeTime = 0;
}
break;
case CharacterState. defending:
…播放防御动画.
if(Input.GetKeyUp(KeyCode.I))
this. heroState = CharacterState. Idling;
break;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
using UnityEngine;
using System.Collections;
using System;
public class InputEventArgs : EventArgs
{
public string input;
public string addition1;
public string addition2;
public InputEventArgs(string _input, string _addition1, string _addition2)
{
this.input = _input;
this.addition1 = _addition1;
this.addition2 = _addition2;
}
}
|
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
|
public delegate void InputEventHandler(object sender, InputEventArgs e);
public event InputEventHandler InputTran;
InputEventArgs inputArgs = new InputEventArgs(,,);
State heroStateActVer;
void Start()
{
personCtrl = this.GetComponent
heroStateActVer = new IdleStateActVer(this.gameObject);
InputTran += new InputEventHandler(heroStateActVer.handleInput);
}
void Input_events(InputEventArgs e)
{
if (e != null)
{
InputTran(this, e);
}
}
void Update()
{
if (Input.anyKeyDown)
{
foreach (char c in Input.inputString)
{
if (c != null)
{
inputArgs.input = c.ToString();
Input_events(inputArgs);
}
}
}
heroStateActVer.UpDate();
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public void handleInput(object sender, InputEventArgs e)
{
input = e.input;
switch (input)
{
case j://攻击
…转为攻击状态..
break;
case i://防御
…转为防御状态…
break;
}
}
|
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
|
using UnityEngine;
using System.Collections;
public class State
{
protected static string input;
protected GameObject Person;
protected Hero Person_ctrl;
protected float chargeTime;
protected float MaxTime;
public State(GameObject _Person)
{
this.Person = _Person;
this.Person_ctrl = _Person.GetComponent
}
public virtual void handleInput(object sender, InputEventArgs e)
{
}
public virtual void UpDate()
{
}
public virtual void UpDate()
{
}
public virtual void Start()
{
}
}
|
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
|
using UnityEngine;
using System.Collections;
public class ActState : State
{
public ActState(GameObject _Person)
: base(_Person)
{
}
public override void Start()
{
this.chargeTime = 0.0f;
this.MaxTime =..攻击动画时间..
..播放攻击动画..
}
public override void handleInput(object sender, InputEventArgs e)
{
input = e.input;
switch (input)
{
case j://连击
if (chargeTime > MaxTime - 0.1f)
{
Person_ctrl.GetActState(1).Start();
}
break;
case i: //转换为防御状态
if (chargeTime > MaxTime - 0.1f)
{
Person_ctrl.SetActState(2);
Person_ctrl.GetNowActState().Start();
}
break;
}
}
public override void UpDate()
{
if (chargeTime < MaxTime)
chargeTime += Time.timeScale / Time.deltaTime;
else
{
this.chargeTime = 0.0f;
Person_ctrl.SetActState(0);
Person_ctrl.GetNowActState().Start();
}
}
}
|
1
2
3
4
5
6
7
8
9
|
using UnityEngine;
using System.Collections;
public class AllState
{
public static State actState = new ActState();
public static State jumpState = new JumpState();
…….
}
|
1
2
3
4
|
private State[] hero_act_state = new State[3];
hero_act_state[0] = new IdleStateActVer(this.gameObject);
hero_act_state[1] = new ActState(this.gameObject);
hero_act_state[2] = new DefenseState(this.gameObject);
|
部分代码已共享至github
命令模式:游戏开发设计模式之命令模式(unity3d 示例实现)
对象池模式:游戏开发设计模式之对象池模式(unity3d 示例实现)
原型模式:游戏开发设计模式之原型模式 & unity3d JSON的使用(unity3d 示例实现)