Browse Source

feat(Scripts): add student's move

tags/0.1.0
mx05 2 years ago
parent
commit
5308117ac6
3 changed files with 105 additions and 15 deletions
  1. +17
    -6
      interface/Assets/Scripts/MapManager.cs
  2. +42
    -9
      interface/Assets/Scripts/MessageReceiver.cs
  3. +46
    -0
      interface/Assets/Scripts/Student.cs

+ 17
- 6
interface/Assets/Scripts/MapManager.cs View File

@@ -8,6 +8,7 @@ public class MapManager : MonoBehaviour
// Start is called before the first frame update // Start is called before the first frame update
private bool mapFinished; private bool mapFinished;
private MessageOfMap map; private MessageOfMap map;
private MessageOfStudent Student;
private int rowCount = 50; private int rowCount = 50;
private int colCount = 50; private int colCount = 50;


@@ -16,6 +17,15 @@ public class MapManager : MonoBehaviour
public GameObject land; public GameObject land;
public GameObject door; public GameObject door;
public GameObject window; public GameObject window;
public GameObject box;
public GameObject book;
public GameObject student_1;
public GameObject student_2;
public GameObject student_3;
public GameObject student_4;
public GameObject Monster_1;
public GameObject Monster_2;
public GameObject Monster_3;
void Start() void Start()
{ {
mapFinished = false; mapFinished = false;
@@ -27,16 +37,16 @@ public class MapManager : MonoBehaviour
if (!mapFinished && MessageReceiver.map != null) if (!mapFinished && MessageReceiver.map != null)
{ {
map = MessageReceiver.map; map = MessageReceiver.map;
Debug.Log("valid map");
//Debug.Log("valid map"); //Debug.Log("valid map");
mapFinished = true; mapFinished = true;
ShowMap(map); ShowMap(map);
} }

} }


private void ShowMap(MessageOfMap map) private void ShowMap(MessageOfMap map)
{ {
var position = new Vector3(-24.5f, 12.25f, 12.25f);
var position = new Vector3(-0.5f, 49.5f, 49.5f);
var block = new GameObject(); var block = new GameObject();
for (int i = 0; i < rowCount; i++) for (int i = 0; i < rowCount; i++)
{ {
@@ -49,10 +59,9 @@ public class MapManager : MonoBehaviour
Instantiate(block, position, new Quaternion(0, 0, 0, 0)); Instantiate(block, position, new Quaternion(0, 0, 0, 0));
} }
} }
position.x = -24.5f;
position.z=position.y = position.y - 0.5f;
position.x = -0.5f;
position.z=position.y = position.y - 1.0f;
} }

} }


private GameObject ShowBlock(PlaceType obj) private GameObject ShowBlock(PlaceType obj)
@@ -66,7 +75,9 @@ public class MapManager : MonoBehaviour
case PlaceType.Door5: return door; case PlaceType.Door5: return door;
case PlaceType.Door6: return door; case PlaceType.Door6: return door;
case PlaceType.Window: return window; case PlaceType.Window: return window;
default:return null;
case PlaceType.Chest:return box;
case PlaceType.Classroom:return book;
default:return land;
} }
} }
} }

+ 42
- 9
interface/Assets/Scripts/MessageReceiver.cs View File

@@ -9,7 +9,7 @@ using System.Data;


public class MessageReceiver : MonoBehaviour public class MessageReceiver : MonoBehaviour
{ {
private bool isMap;
private static int studentNum = 2;
// Start is called before the first frame update // Start is called before the first frame update
async void Start() async void Start()
{ {
@@ -17,18 +17,50 @@ public class MessageReceiver : MonoBehaviour
var client = new AvailableService.AvailableServiceClient(channel); var client = new AvailableService.AvailableServiceClient(channel);
PlayerMsg msg = new PlayerMsg(); PlayerMsg msg = new PlayerMsg();
msg.PlayerType = PlayerType.StudentPlayer; msg.PlayerType = PlayerType.StudentPlayer;
msg.PlayerId = 0;
isMap = true;
msg.PlayerId = 3000;
map = null; map = null;
var response = client.AddPlayer(msg); var response = client.AddPlayer(msg);
while (await response.ResponseStream.MoveNext())
//while (await response.ResponseStream.MoveNext())
//{
// var responseVal = response.ResponseStream.Current;
// if (isMap)
// {
// map = responseVal.ObjMessage[0].MapMessage;
// //Debug.Log(map.ToString());
// isMap = false;
// for (int i = 0; i < studentNum; i++)
// Instantiate(student_1, new Vector3(0f, 0f, 10.0f), new Quaternion(0, 0, 0, 0));
// }
// else
// {
// for (int i = 0; i < studentNum; i++)
// {
// Student[i] = responseVal.ObjMessage[i].StudentMessage;
// }


// }
//}
if(await response.ResponseStream.MoveNext())
{
var responseVal = response.ResponseStream.Current;
map = responseVal.ObjMessage[0].MapMessage;
}
if (await response.ResponseStream.MoveNext())
{ {
var responseVal = response.ResponseStream.Current; var responseVal = response.ResponseStream.Current;
if (isMap)
for (int i = 0; i < studentNum; i++)
{ {
map = responseVal.ObjMessage[0].MapMessage;
//Debug.Log(map.ToString());
isMap = false;
Student[i] = responseVal.ObjMessage[i].StudentMessage;
Instantiate(student_1, new Vector3(0f, 0f, 10.0f), new Quaternion(0, 0, 0, 0));
}
}
while(await response.ResponseStream.MoveNext())
{
var responseVal = response.ResponseStream.Current;
for(int i=0;i<studentNum;i++)
{
Student[i] = responseVal.ObjMessage[i].StudentMessage;
} }
} }
} }
@@ -36,6 +68,7 @@ public class MessageReceiver : MonoBehaviour
void Update() void Update()
{ {
} }

public static MessageOfMap map; public static MessageOfMap map;
public static MessageOfStudent[] Student = new MessageOfStudent[studentNum];
public GameObject student_1;
} }

+ 46
- 0
interface/Assets/Scripts/Student.cs View File

@@ -0,0 +1,46 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Grpc.Core;
using Google.Protobuf;
using Protobuf;
using System.Data;

public class Student : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
num = studentCount;
studentCount++;
anim = GetComponent<Animator>();
occupation = MessageReceiver.Student[num].StudentType;
transform.position = lastPosition = new Vector3(MessageReceiver.Student[num].Y / 1000.0f,
50.0f - MessageReceiver.Student[num].X / 1000.0f,
50.0f - MessageReceiver.Student[num].X / 1000.0f - 0.5f);
}

// Update is called once per frame
void Update()
{
var currentPosition = new Vector3(MessageReceiver.Student[num].Y / 1000.0f,
50.0f - MessageReceiver.Student[num].X / 1000.0f,
50.0f - MessageReceiver.Student[num].X / 1000.0f - 0.5f);
Vector3 step = currentPosition - lastPosition;
if (step != Vector3.zero)
{
transform.position = currentPosition;
anim.SetBool("isRun", true);
}
else
{
anim.SetBool("isRun", false);
}
lastPosition = currentPosition;
}
private int num;
private StudentType occupation;
private Vector3 lastPosition = new Vector3(0, 0, 10.0f);
private static int studentCount = 0;
private Animator anim;
}

Loading…
Cancel
Save