Skip to content

Commit

Permalink
feat(Scripts): ✨ add student's move
Browse files Browse the repository at this point in the history
  • Loading branch information
M-X-05 committed Apr 21, 2023
1 parent 6b77151 commit 5308117
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 15 deletions.
23 changes: 17 additions & 6 deletions interface/Assets/Scripts/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class MapManager : MonoBehaviour
// Start is called before the first frame update
private bool mapFinished;
private MessageOfMap map;
private MessageOfStudent Student;
private int rowCount = 50;
private int colCount = 50;

Expand All @@ -16,6 +17,15 @@ public class MapManager : MonoBehaviour
public GameObject land;
public GameObject door;
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()
{
mapFinished = false;
Expand All @@ -27,16 +37,16 @@ void Update()
if (!mapFinished && MessageReceiver.map != null)
{
map = MessageReceiver.map;
Debug.Log("valid map");
//Debug.Log("valid map");
mapFinished = true;
ShowMap(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();
for (int i = 0; i < rowCount; i++)
{
Expand All @@ -49,10 +59,9 @@ private void ShowMap(MessageOfMap map)
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)
Expand All @@ -66,7 +75,9 @@ private GameObject ShowBlock(PlaceType obj)
case PlaceType.Door5: return door;
case PlaceType.Door6: return door;
case PlaceType.Window: return window;
default:return null;
case PlaceType.Chest:return box;
case PlaceType.Classroom:return book;
default:return land;
}
}
}
51 changes: 42 additions & 9 deletions interface/Assets/Scripts/MessageReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,66 @@

public class MessageReceiver : MonoBehaviour
{
private bool isMap;
private static int studentNum = 2;
// Start is called before the first frame update
async void Start()
{
var channel = new Channel("127.0.0.1:8888", ChannelCredentials.Insecure);
var client = new AvailableService.AvailableServiceClient(channel);
PlayerMsg msg = new PlayerMsg();
msg.PlayerType = PlayerType.StudentPlayer;
msg.PlayerId = 0;
isMap = true;
msg.PlayerId = 3000;
map = null;
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;
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;
}
}
}
// Update is called once per frame
void Update()
{
}

public static MessageOfMap map;
public static MessageOfStudent[] Student = new MessageOfStudent[studentNum];
public GameObject student_1;
}
46 changes: 46 additions & 0 deletions interface/Assets/Scripts/Student.cs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 5308117

Please sign in to comment.