File size: 2,530 Bytes
0988e96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
-- Avatar.lua

local Avatar = {}
Avatar.__index = Avatar

function Avatar:new(config)
    local obj = {
        name        = config.name or "Player",
        x           = config.x or 0,
        y           = config.y or 0,
        speed       = config.speed or 100,   -- Einheiten pro Sekunde
        direction   = "down",                -- "up", "down", "left", "right"
        state       = "idle",                -- "idle", "walk", "attack", ...
        sprite      = config.sprite or nil,  -- Referenz auf Bild/Sprite-Objekt
        frame       = 1,
        frameTime   = 0,
        frameSpeed  = config.frameSpeed or 0.1,
        animations  = config.animations or {}
    }

    setmetatable(obj, Avatar)
    return obj
end

-- Richtung und Zustand setzen
function Avatar:setState(state, direction)
    if state then self.state = state end
    if direction then self.direction = direction end
    self.frame = 1
    self.frameTime = 0
end

-- Bewegen
function Avatar:move(dx, dy, dt)
    if dx == 0 and dy == 0 then
        self:setState("idle")
        return
    end

    self.x = self.x + dx * self.speed * dt
    self.y = self.y + dy * self.speed * dt

    -- Richtung automatisch setzen
    if math.abs(dx) > math.abs(dy) then
        self.direction = dx > 0 and "right" or "left"
    else
        self.direction = dy > 0 and "down" or "up"
    end

    self:setState("walk", self.direction)
end

-- Animation updaten
function Avatar:update(dt)
    local animKey = self.state .. "_" .. self.direction
    local anim = self.animations[animKey]

    if anim then
        self.frameTime = self.frameTime + dt
        if self.frameTime >= self.frameSpeed then
            self.frameTime = self.frameTime - self.frameSpeed
            self.frame = self.frame + 1
            if self.frame > #anim then
                self.frame = 1
            end
        end
    end
end

-- Zeichnen / Rendern (Pseudo-Code, abhängig von Engine)
function Avatar:draw()
    if not self.sprite then
        -- Fallback: simple Platzhalter-Darstellung
        -- z.B. mit Love2D: love.graphics.rectangle("line", self.x, self.y, 32, 32)
        return
    end

    local animKey = self.state .. "_" .. self.direction
    local anim = self.animations[animKey]

    if anim then
        local quad = anim[self.frame]
        -- Beispiel mit Love2D:
        -- love.graphics.draw(self.sprite, quad, self.x, self.y)
    else
        -- Standardframe zeichnen
        -- love.graphics.draw(self.sprite, self.x, self.y)
    end
end

return Avatar