MCAce commited on
Commit
0988e96
·
verified ·
1 Parent(s): b4a7e65

Avatar.lua

Browse files
Files changed (1) hide show
  1. Avatar.lua +91 -0
Avatar.lua ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -- Avatar.lua
2
+
3
+ local Avatar = {}
4
+ Avatar.__index = Avatar
5
+
6
+ function Avatar:new(config)
7
+ local obj = {
8
+ name = config.name or "Player",
9
+ x = config.x or 0,
10
+ y = config.y or 0,
11
+ speed = config.speed or 100, -- Einheiten pro Sekunde
12
+ direction = "down", -- "up", "down", "left", "right"
13
+ state = "idle", -- "idle", "walk", "attack", ...
14
+ sprite = config.sprite or nil, -- Referenz auf Bild/Sprite-Objekt
15
+ frame = 1,
16
+ frameTime = 0,
17
+ frameSpeed = config.frameSpeed or 0.1,
18
+ animations = config.animations or {}
19
+ }
20
+
21
+ setmetatable(obj, Avatar)
22
+ return obj
23
+ end
24
+
25
+ -- Richtung und Zustand setzen
26
+ function Avatar:setState(state, direction)
27
+ if state then self.state = state end
28
+ if direction then self.direction = direction end
29
+ self.frame = 1
30
+ self.frameTime = 0
31
+ end
32
+
33
+ -- Bewegen
34
+ function Avatar:move(dx, dy, dt)
35
+ if dx == 0 and dy == 0 then
36
+ self:setState("idle")
37
+ return
38
+ end
39
+
40
+ self.x = self.x + dx * self.speed * dt
41
+ self.y = self.y + dy * self.speed * dt
42
+
43
+ -- Richtung automatisch setzen
44
+ if math.abs(dx) > math.abs(dy) then
45
+ self.direction = dx > 0 and "right" or "left"
46
+ else
47
+ self.direction = dy > 0 and "down" or "up"
48
+ end
49
+
50
+ self:setState("walk", self.direction)
51
+ end
52
+
53
+ -- Animation updaten
54
+ function Avatar:update(dt)
55
+ local animKey = self.state .. "_" .. self.direction
56
+ local anim = self.animations[animKey]
57
+
58
+ if anim then
59
+ self.frameTime = self.frameTime + dt
60
+ if self.frameTime >= self.frameSpeed then
61
+ self.frameTime = self.frameTime - self.frameSpeed
62
+ self.frame = self.frame + 1
63
+ if self.frame > #anim then
64
+ self.frame = 1
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ -- Zeichnen / Rendern (Pseudo-Code, abhängig von Engine)
71
+ function Avatar:draw()
72
+ if not self.sprite then
73
+ -- Fallback: simple Platzhalter-Darstellung
74
+ -- z.B. mit Love2D: love.graphics.rectangle("line", self.x, self.y, 32, 32)
75
+ return
76
+ end
77
+
78
+ local animKey = self.state .. "_" .. self.direction
79
+ local anim = self.animations[animKey]
80
+
81
+ if anim then
82
+ local quad = anim[self.frame]
83
+ -- Beispiel mit Love2D:
84
+ -- love.graphics.draw(self.sprite, quad, self.x, self.y)
85
+ else
86
+ -- Standardframe zeichnen
87
+ -- love.graphics.draw(self.sprite, self.x, self.y)
88
+ end
89
+ end
90
+
91
+ return Avatar