Spiel mit UI, unterschiedlichen Funktionen auf Blöcken und einer physikalischen Bewegung. Ein paar Bilder sind auch dabei.

This commit is contained in:
doz_neuhaus
2025-08-18 01:15:23 +02:00
parent b6901da58a
commit ba6076bfd7
141 changed files with 67782 additions and 221 deletions

View File

@@ -0,0 +1,71 @@
using UnityEngine;
using UnityEngine.Events;
public class ContactAction : MonoBehaviour
{
public bool isCollisionActive; // Ob man wirklich mit dem Ding physisch kollidiert (oder ob man durch kann)
public UnityEvent onContact;
private Collider2D colliderComponent;
private GameObject thingThatCollided; // wird wohl meist der Spieler sein
private TextNote textNote;
private void Awake()
{
colliderComponent = this.GetComponent<Collider2D>();
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
colliderComponent.isTrigger = !isCollisionActive;
}
public void Kill()
{
if (thingThatCollided.GetComponent<Player>() != null)
{
thingThatCollided.GetComponent<Player>().Kill();
}
}
public void Yeet()
{
if (thingThatCollided.GetComponent<Rigidbody2D>() != null)
{
thingThatCollided.GetComponent<Rigidbody2D>().AddForce(Vector2.up * 10f, ForceMode2D.Impulse);
}
}
public void ShowText()
{
if (this.gameObject.GetComponent<TextNote>() != null)
{
textNote = this.gameObject.GetComponent<TextNote>();
GameManager.gameManager.ShowText(textNote.headline, textNote.content);
}
else
{
Debug.Log("keine Component TextNote vorhanden");
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (isCollisionActive)
{
thingThatCollided = collision.gameObject;
onContact.Invoke();
}
}
private void OnTriggerEnter2D(Collider2D collider)
{
if (!isCollisionActive)
{
thingThatCollided = collider.gameObject;
onContact.Invoke();
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 70869a6e5c8e0b942a0912e8430bc0e7

55
Assets/Scripts/Player.cs Normal file
View File

@@ -0,0 +1,55 @@
using UnityEngine;
public class Player : MonoBehaviour
{
public Transform spawnPoint;
[SerializeField]
private int food;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnInteract()
{
if (UseFood())
{
}
}
public void FindFood()
{
food++;
}
public bool UseFood()
{
bool hasEaten = true;
if (food > 0)
{
food--;
}
else
{
Debug.Log("i'm starving");
hasEaten = false;
}
return hasEaten;
}
public void Kill()
{
this.GetComponent<Rigidbody2D>().linearVelocity = Vector2.zero;
this.gameObject.transform.position = spawnPoint.position;
Debug.Log("Player died");
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: c8b3b3fd9c8a41c4496019ca1e644f83

View File

@@ -0,0 +1,102 @@
using System;
using System.Diagnostics.Tracing;
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.U2D;
public class PlayerMovement : MonoBehaviour
{
[SerializeField]
private float movePower;
[SerializeField]
private float jumpPower;
[SerializeField]
[Range(0, 2)]
private float breakingPower;
[SerializeField]
private float maxSpeed;
public bool isGrounded;
public bool shallJump;
public float direction; // float statt Vector2, weil wir nur links/rechts steuern wollen
private Rigidbody2D rb;
[SerializeField]
private ContactFilter2D contactFilter;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate()
{
isGrounded = GroundCheck();
// Springen nur bei Bodenkontakt
if (isGrounded && shallJump)
{
rb.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
shallJump = false;
isGrounded = false;
}
// Bremsen, wenn Controller auf 0 oder in entgegengesetzte Richtung:
if ((direction <= 0.05f && direction > -0.05f))
//|| (rb.linearVelocity.x > 0f && direction < 0f)
//|| (rb.linearVelocity.x < 0f && direction > 0f))
{
if (breakingPower > 1f || breakingPower < 0f)
{
rb.linearVelocity = new Vector2(0f, rb.linearVelocityY);
}
else
{
rb.AddForce(new Vector2(-rb.linearVelocityX, 0f) * breakingPower * movePower, ForceMode2D.Force);
}
}
else
{
// Beschleunigen
rb.AddForce(Vector2.right * direction * movePower, ForceMode2D.Force);
// bis zu einer gewissen Geschwindigkeit
if (rb.linearVelocity.magnitude >= maxSpeed)
{
rb.linearVelocity = rb.linearVelocity.normalized * maxSpeed;
}
}
}
private bool GroundCheck()
{
return rb.IsTouching(contactFilter);
}
// wird von PlayerInput aufgerufen, wenn das auf "SendMessage" steht.
public void OnMove(InputValue value)
{
direction = value.Get<Vector2>().x; // nur rechts-links
if (direction > 0)
{
this.gameObject.transform.localScale = new Vector3(-1f, 1, 1); // sprite schaut entgegengesetzt
}
else if(direction < 0)
{
this.gameObject.transform.localScale = new Vector3(1f, 1, 1); // sprite schaut in seine normale Richtung
}
}
public void OnJump()
{
if (isGrounded)
{
shallJump = true;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9b90ab1ab6325b84fafc369d44fe7a15